pi-roundtable 0.52.0 → 0.52.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.
@@ -27,7 +27,11 @@ import { tellDeleted, tellEdited } from "./claim-events.ts";
27
27
  import { ForwardJoin } from "./forward-join.ts";
28
28
  import type { MessageObservers } from "./message-observers.ts";
29
29
  import { type PeerBots, peerFacts } from "./peer-bots.ts";
30
- import { type QueuedEntry, QueuedMessages } from "./queued-messages.ts";
30
+ import {
31
+ type QueuedEntry,
32
+ QueuedMessages,
33
+ type RefuseTally,
34
+ } from "./queued-messages.ts";
31
35
 
32
36
  export interface ChannelRouterOptions {
33
37
  /** Claims in registration order; the router orders them by priority. */
@@ -248,16 +252,23 @@ export class ChannelRouter implements ConversationPort {
248
252
  reason: new HostStoppingError().message,
249
253
  });
250
254
  logger.info({ channel }, "message not run: the host is shutting down");
251
- if (message.authorIsBot || message.integration || this.#told.has(channel))
252
- return;
253
- this.#told.add(channel);
255
+ if (message.authorIsBot || message.integration) return;
254
256
  try {
255
- await surfaces?.sendReply?.(channel, {
256
- chunks: [messages().restartingNotice],
257
- });
257
+ // The surface may take it, and tell its channel itself.
258
+ if (await surfaces?.of(channel)?.refuseStopping?.(message)) return;
258
259
  } catch (error) {
259
- logger.warn({ channel, err: error }, "restart notice not posted");
260
+ logger.warn(
261
+ { channel, err: error },
262
+ "surface did not take a refused message",
263
+ );
260
264
  }
265
+ if (this.#told.has(channel)) return;
266
+ this.#told.add(channel);
267
+ await surfaces
268
+ ?.sendReply?.(channel, { chunks: [messages().restartingNotice] })
269
+ .catch((error: unknown) =>
270
+ logger.warn({ channel, err: error }, "restart notice not posted"),
271
+ );
261
272
  }
262
273
 
263
274
  /**
@@ -278,14 +289,15 @@ export class ChannelRouter implements ConversationPort {
278
289
  }
279
290
  const { busy } = admission;
280
291
  let waiting: QueuedEntry | undefined;
281
- // A message during a turn is added to it when the turn takes it, otherwise marked as waiting.
292
+ // During a turn a message joins it or is marked waiting; unmarkable, it is kept so a shutdown refuses it.
282
293
  if (busy && queue.size(message.channel) > 0) {
283
294
  if (await busy.steer?.()) return { done: busy.react(STEERED_MARK) };
284
295
  // Listed before the mark is placed, so a delete or cancel during that wait reaches it.
285
296
  waiting = this.#queued.add(claim, message, admission);
286
297
  await busy.react(QUEUED_MARK);
287
298
  if (waiting.cancelled) return { done: Promise.resolve() };
288
- }
299
+ } else if (queue.size(message.channel) > 0)
300
+ waiting = this.#queued.add(claim, message, admission, false);
289
301
  const done = queue
290
302
  .run(message.channel, () => {
291
303
  if (!waiting) return admission.run();
@@ -313,6 +325,14 @@ export class ChannelRouter implements ConversationPort {
313
325
  return { done };
314
326
  }
315
327
 
328
+ /** Refuses each message still waiting behind a turn after the drain, counting in `tally`. */
329
+ refuseWaiting(tally: RefuseTally): Promise<void> {
330
+ return this.#queued.refuseAll(
331
+ (e) => this.#refuse(e.message, e.admission),
332
+ tally,
333
+ );
334
+ }
335
+
316
336
  queued(channel: ChannelKey): readonly QueuedMessage[] {
317
337
  return this.#queued.list(channel);
318
338
  }
@@ -340,7 +360,7 @@ export class ChannelRouter implements ConversationPort {
340
360
  messageId: string,
341
361
  space?: string,
342
362
  ): Promise<void> {
343
- // The surface says it is gone: no author check, and no mark to remove from a deleted message.
363
+ // The surface says it is gone: no author check, no mark to remove; an unmarked one still runs, as ever.
344
364
  this.#queued.cancel(channel, messageId, undefined, false);
345
365
  return tellDeleted(
346
366
  this.#owner(channel, space),
@@ -21,6 +21,15 @@ export interface QueuedEntry {
21
21
  readonly queuedAt: number;
22
22
  /** Cancelled while it waited: its turn does not run. */
23
23
  cancelled: boolean;
24
+ /** Marked as waiting, so its author may see, cancel, or edit it; an unmarked one is only refused at shutdown. */
25
+ readonly listed: boolean;
26
+ }
27
+
28
+ /** How a refusal of every waiting message went, filled in as it goes; set `stopped` to start no more. */
29
+ export interface RefuseTally {
30
+ refused: number;
31
+ failed: number;
32
+ stopped: boolean;
24
33
  }
25
34
 
26
35
  /**
@@ -43,6 +52,7 @@ export class QueuedMessages {
43
52
  claim: ChannelClaim,
44
53
  message: InboundMessage,
45
54
  admission: TurnAdmission,
55
+ listed = true,
46
56
  ): QueuedEntry {
47
57
  const entry: QueuedEntry = {
48
58
  claim,
@@ -50,6 +60,7 @@ export class QueuedMessages {
50
60
  admission,
51
61
  queuedAt: this.#now(),
52
62
  cancelled: false,
63
+ listed,
53
64
  };
54
65
  const channel = this.#entries.get(message.channel) ?? new Map();
55
66
  channel.set(message.messageId, entry);
@@ -68,14 +79,16 @@ export class QueuedMessages {
68
79
  }
69
80
 
70
81
  list(channel: ChannelKey): readonly QueuedMessage[] {
71
- return [...(this.#entries.get(channel)?.values() ?? [])].map((entry) => ({
72
- messageId: entry.message.messageId,
73
- ...(entry.message.speaker
74
- ? { principalId: entry.message.speaker.principalId }
75
- : {}),
76
- text: entry.message.text,
77
- queuedAt: entry.queuedAt,
78
- }));
82
+ return [...(this.#entries.get(channel)?.values() ?? [])]
83
+ .filter((entry) => entry.listed)
84
+ .map((entry) => ({
85
+ messageId: entry.message.messageId,
86
+ ...(entry.message.speaker
87
+ ? { principalId: entry.message.speaker.principalId }
88
+ : {}),
89
+ text: entry.message.text,
90
+ queuedAt: entry.queuedAt,
91
+ }));
79
92
  }
80
93
 
81
94
  /**
@@ -89,7 +102,7 @@ export class QueuedMessages {
89
102
  by: QueuedBy | undefined,
90
103
  unmark: boolean,
91
104
  ): QueuedChange {
92
- const entry = this.#entries.get(channel)?.get(messageId);
105
+ const entry = this.#listed(channel, messageId);
93
106
  if (!entry) return "unknown";
94
107
  if (by && !this.#authored(entry, by)) return "forbidden";
95
108
  this.remove(entry);
@@ -109,7 +122,7 @@ export class QueuedMessages {
109
122
  by: QueuedBy | undefined,
110
123
  text: string,
111
124
  ): QueuedChange {
112
- const entry = this.#entries.get(channel)?.get(messageId);
125
+ const entry = this.#listed(channel, messageId);
113
126
  if (!entry) return "unknown";
114
127
  if (by && !this.#authored(entry, by)) return "forbidden";
115
128
  if (entry.message.text === text) return "done";
@@ -131,6 +144,32 @@ export class QueuedMessages {
131
144
  return "done";
132
145
  }
133
146
 
147
+ /** Takes every waiting entry off the list and refuses it, the channels in parallel, each in order, until `tally.stopped`. */
148
+ async refuseAll(
149
+ refuse: (entry: QueuedEntry) => Promise<void>,
150
+ tally: RefuseTally,
151
+ ): Promise<void> {
152
+ await Promise.all(
153
+ [...this.#entries.values()].map(async (entries) => {
154
+ for (const entry of [...entries.values()]) {
155
+ if (tally.stopped) return;
156
+ if (!this.remove(entry)) continue;
157
+ this.unmark(entry);
158
+ try {
159
+ await refuse(entry);
160
+ tally.refused += 1;
161
+ } catch (error) {
162
+ tally.failed += 1;
163
+ this.#logger.warn(
164
+ { channel: entry.message.channel, err: error },
165
+ "a waiting message could not be refused",
166
+ );
167
+ }
168
+ }
169
+ }),
170
+ );
171
+ }
172
+
134
173
  /** Removes the waiting mark; a failure is logged, since the message may be gone. */
135
174
  unmark(entry: QueuedEntry): void {
136
175
  const { channel } = entry.message;
@@ -144,6 +183,11 @@ export class QueuedMessages {
144
183
  );
145
184
  }
146
185
 
186
+ #listed(channel: ChannelKey, messageId: string): QueuedEntry | undefined {
187
+ const entry = this.#entries.get(channel)?.get(messageId);
188
+ return entry?.listed ? entry : undefined;
189
+ }
190
+
147
191
  #authored(entry: QueuedEntry, by: QueuedBy): boolean {
148
192
  const author = entry.message.speaker?.principalId;
149
193
  return author !== undefined && author === by.principalId;