pi-roundtable 0.52.1 → 0.52.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -1
- package/README.md +1 -0
- package/docs/plugins.md +9 -5
- package/package.json +1 -1
- package/src/core/agents/agent-messages.ts +79 -28
- package/src/core/agents/agent-outbox.ts +6 -3
- package/src/core/agents/team-editing.ts +8 -6
- package/src/core/agents/team-turns.ts +4 -2
- package/src/core/config/limits.ts +20 -1
- package/src/core/contract/channels.ts +2 -1
- package/src/core/discord/catch-up-gate.ts +24 -11
- package/src/core/discord/catch-up-journal.ts +4 -4
- package/src/core/host/define-roundtable.ts +5 -1
- package/src/core/host/drain.ts +1 -3
- package/src/core/host/host-registration.ts +2 -0
- package/src/core/host/host.ts +34 -13
- package/src/core/routing/admit-message.ts +75 -0
- package/src/core/routing/channel-queue.ts +11 -2
- package/src/core/routing/channel-router.ts +79 -93
- package/src/core/routing/conversation-turns.ts +103 -93
- package/src/core/routing/queued-messages.ts +27 -16
- package/src/core/routing/settle-turn.ts +38 -5
- package/src/core/routing/stopping-notice.ts +39 -0
- package/src/core/routing/turn-records.ts +341 -0
package/src/core/host/host.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { SQL } from "bun";
|
|
2
2
|
import type { Hex } from "../../characters/index.ts";
|
|
3
3
|
import type { AttachmentRetention } from "../attachments/attachment-retention.ts";
|
|
4
|
+
import { REFUSE_WAITING_MS } from "../config/limits.ts";
|
|
4
5
|
import type { PeerBotsConfig } from "../config/peer-bots.ts";
|
|
5
6
|
import { checkDatabase } from "../db/check-database.ts";
|
|
6
7
|
import { openPool, runMigrations } from "../db/migrations.ts";
|
|
@@ -100,9 +101,10 @@ export interface RoundtableOptions {
|
|
|
100
101
|
/**
|
|
101
102
|
* The shutdown drain: it waits up to `limitMs` (3 minutes by default) for the turns already
|
|
102
103
|
* running, then stops them, gives them `abortGraceMs` (10 seconds) to end, and goes on without
|
|
103
|
-
* them. Nothing new starts once it begins.
|
|
104
|
+
* them. Nothing new starts once it begins. The messages still waiting behind a turn then get
|
|
105
|
+
* `refuseWaitingMs` (15 seconds) to be refused. Tests shorten the limits and the clock.
|
|
104
106
|
*/
|
|
105
|
-
drain?: Omit<DrainOptions, "busy"
|
|
107
|
+
drain?: Omit<DrainOptions, "busy"> & { refuseWaitingMs?: number };
|
|
106
108
|
/** What `listen()` calls with the shutdown's exit code; the process's own exit by default. */
|
|
107
109
|
exit?: (code: number) => void;
|
|
108
110
|
}
|
|
@@ -110,9 +112,6 @@ export interface RoundtableOptions {
|
|
|
110
112
|
/** The host running in this process; the text catalog, time zone and environment are process-wide. */
|
|
111
113
|
let running: Roundtable | undefined;
|
|
112
114
|
|
|
113
|
-
/** How long the messages still waiting after the drain are given to be refused, before the teardown. */
|
|
114
|
-
const REFUSE_WAITING_MS = 15_000;
|
|
115
|
-
|
|
116
115
|
/** `work`, or a rejection once `ms` pass; the timer never keeps the process alive. */
|
|
117
116
|
async function withinMs<T>(ms: number, work: Promise<T> | T): Promise<T> {
|
|
118
117
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -300,42 +299,64 @@ export class Roundtable {
|
|
|
300
299
|
logger.info({ signal }, "shutting down once idle");
|
|
301
300
|
this.#link.queue.close();
|
|
302
301
|
beginServicesDrain(this.#started, logger);
|
|
302
|
+
// The queue's channels, and those where a message's turn runs outside it.
|
|
303
|
+
const turnChannels = () => [
|
|
304
|
+
...new Set([
|
|
305
|
+
...this.#link.queue.busy(),
|
|
306
|
+
...(this.#link.router?.busy() ?? []),
|
|
307
|
+
]),
|
|
308
|
+
];
|
|
303
309
|
const stopQueuedTurns = () =>
|
|
304
310
|
stopTurns(
|
|
305
|
-
|
|
306
|
-
(channel) => this.#link.router?.
|
|
311
|
+
turnChannels(),
|
|
312
|
+
(channel) => this.#link.router?.abort(channel as ChannelKey) ?? false,
|
|
307
313
|
logger,
|
|
308
314
|
);
|
|
309
315
|
const left = await drainWork({
|
|
310
316
|
...drain,
|
|
311
317
|
busy: () => [
|
|
312
|
-
...
|
|
318
|
+
...turnChannels(),
|
|
313
319
|
...this.#started.flatMap((service) => service.busy?.() ?? []),
|
|
314
320
|
],
|
|
315
321
|
// A turn the limit finds still running is stopped, as the owner's stop would, and a
|
|
316
322
|
// service is told to stop its own work before it is stopped.
|
|
317
323
|
abort: () => {
|
|
324
|
+
// What still runs now was cut short: unless it answers in the grace, its messages are left.
|
|
325
|
+
this.#link.router?.cutRunning();
|
|
318
326
|
const turns = stopQueuedTurns();
|
|
319
327
|
const services = abortServices(this.#started, logger);
|
|
320
328
|
return turns || services;
|
|
321
329
|
},
|
|
322
330
|
});
|
|
323
|
-
// Only what ran before the drain ended finishes; nothing delivered later starts.
|
|
331
|
+
// Only what ran before the drain ended finishes; nothing delivered later starts. A message's
|
|
332
|
+
// turn still running that the drain did not count, such as one a `finishing` delivery routed, is cut now.
|
|
324
333
|
this.#link.queue.seal();
|
|
325
|
-
|
|
334
|
+
this.#link.router?.cutRunning();
|
|
335
|
+
// A cut turn that did not answer, then a message still waiting behind a turn, never answers:
|
|
336
|
+
// their channels are told while the services run, the cut first, as its message came first.
|
|
326
337
|
// Counted as it goes, and told to start no more once the limit passes.
|
|
327
338
|
const tally = { refused: 0, failed: 0, stopped: false };
|
|
339
|
+
const router = this.#link.router;
|
|
328
340
|
const told = await attempt(() =>
|
|
329
|
-
withinMs(
|
|
341
|
+
withinMs(
|
|
342
|
+
drain?.refuseWaitingMs ?? REFUSE_WAITING_MS,
|
|
343
|
+
router?.leaveCut(tally).then(async ({ dropping }) => {
|
|
344
|
+
await router.refuseWaiting(tally);
|
|
345
|
+
await dropping;
|
|
346
|
+
}),
|
|
347
|
+
),
|
|
330
348
|
);
|
|
331
349
|
tally.stopped = true;
|
|
332
350
|
if (!told.ok || tally.failed > 0)
|
|
333
351
|
logger.error(
|
|
334
352
|
{ err: told.ok ? undefined : told.error, ...tally },
|
|
335
|
-
"not every waiting message was refused; the rest are not answered",
|
|
353
|
+
"not every cut or waiting message was refused; the rest are not answered",
|
|
336
354
|
);
|
|
337
355
|
else if (tally.refused > 0)
|
|
338
|
-
logger.info(
|
|
356
|
+
logger.info(
|
|
357
|
+
{ refused: tally.refused },
|
|
358
|
+
"cut and waiting messages refused",
|
|
359
|
+
);
|
|
339
360
|
// A turn that started during the abort grace, a report's, is stopped too, so the teardown does not wait for it.
|
|
340
361
|
if (left.length > 0) stopQueuedTurns();
|
|
341
362
|
if (left.length > 0) {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Admission,
|
|
3
|
+
ChannelClaim,
|
|
4
|
+
InboundMessage,
|
|
5
|
+
} from "../contract/channels.ts";
|
|
6
|
+
import type { Contact, ContactAssessor } from "../identity/contact.ts";
|
|
7
|
+
import type { Logger } from "../kernel/log.ts";
|
|
8
|
+
import { actorOf } from "./actor-of.ts";
|
|
9
|
+
|
|
10
|
+
export interface AdmitOptions {
|
|
11
|
+
logger: Logger;
|
|
12
|
+
/** Who the authors of messages are; without it, no message carries a speaker. */
|
|
13
|
+
contacts?: ContactAssessor;
|
|
14
|
+
/** The surfaces already warned that their messages carry no actor. */
|
|
15
|
+
warned: Set<string>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The claim's admission of the message, which carries the speaker its author resolves to; one
|
|
20
|
+
* who cannot be resolved, such as while the database is down, reaches the claim as no one. The
|
|
21
|
+
* author's contact is recorded, such as their first contact linked, only once the claim takes
|
|
22
|
+
* the message, so a message nobody serves links or makes no one. A linked author the rules
|
|
23
|
+
* refuse is still recorded as seen at no tier as the message is assessed, whether or not a
|
|
24
|
+
* claim then takes it, at most once in a while. When that record fails, or finds them someone
|
|
25
|
+
* else or no one now, such as linked elsewhere meanwhile, the message is dropped: it would run
|
|
26
|
+
* as a principal that may not exist, such as one only assessed at a first contact. The claim
|
|
27
|
+
* hears it through the admission's `dropped` or `unanswered`.
|
|
28
|
+
*/
|
|
29
|
+
export async function admitMessage(
|
|
30
|
+
claim: ChannelClaim,
|
|
31
|
+
message: InboundMessage,
|
|
32
|
+
{ logger, contacts, warned }: AdmitOptions,
|
|
33
|
+
): Promise<{ admission: Admission; message: InboundMessage } | undefined> {
|
|
34
|
+
const { speaker: _given, ...facts } = message;
|
|
35
|
+
const actor = actorOf(message, warned, logger);
|
|
36
|
+
let contact: Contact | undefined;
|
|
37
|
+
if (actor && contacts)
|
|
38
|
+
try {
|
|
39
|
+
contact = await contacts.assess(actor, {
|
|
40
|
+
conversation: message.channel,
|
|
41
|
+
});
|
|
42
|
+
} catch (error) {
|
|
43
|
+
logger.error(
|
|
44
|
+
{ channel: message.channel, err: error },
|
|
45
|
+
"could not resolve who wrote a message; it reaches its claim as no one",
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const admittedAs: InboundMessage = contact
|
|
49
|
+
? { ...facts, speaker: Object.freeze({ ...contact.speaker }) }
|
|
50
|
+
: facts;
|
|
51
|
+
const admission = claim.admit(admittedAs);
|
|
52
|
+
if (!admission) return undefined;
|
|
53
|
+
if (!contact) return { admission, message: admittedAs };
|
|
54
|
+
try {
|
|
55
|
+
const taken = await contact.take();
|
|
56
|
+
if (taken?.principalId === contact.speaker.principalId)
|
|
57
|
+
return { admission, message: admittedAs };
|
|
58
|
+
logger.warn(
|
|
59
|
+
{ channel: message.channel },
|
|
60
|
+
"the author of a message was linked elsewhere, or refused, while it was admitted; it does not run",
|
|
61
|
+
);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
logger.error(
|
|
64
|
+
{ channel: message.channel, err: error },
|
|
65
|
+
"could not record who wrote a message; it does not run",
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
if (admission.kind === "turn") admission.dropped?.();
|
|
69
|
+
else
|
|
70
|
+
admission.unanswered({
|
|
71
|
+
status: "skipped",
|
|
72
|
+
reason: "its author was not who they were when it was admitted",
|
|
73
|
+
});
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import type { ChannelKey } from "../domain/sessions.ts";
|
|
3
3
|
import { HostStoppingError } from "../kernel/errors.ts";
|
|
4
|
+
import { currentRecord, inChannel } from "./turn-records.ts";
|
|
4
5
|
|
|
5
6
|
/** Runs tasks one at a time per channel, in arrival order; different channels run independently. */
|
|
6
7
|
export class ChannelQueue {
|
|
@@ -76,11 +77,18 @@ export class ChannelQueue {
|
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
run<T>(channel: ChannelKey, task: () => Promise<T>): Promise<T> {
|
|
80
|
+
// Queued from a message's admission, it keeps that admission's turn record open until it ends,
|
|
81
|
+
// so a turn it runs after the admission returned still answers or leaves that message.
|
|
82
|
+
const record = currentRecord();
|
|
83
|
+
record?.begin(channel);
|
|
79
84
|
const admitted = this.#admitted();
|
|
80
85
|
const start = () => {
|
|
81
|
-
if (this.#closed && !(admitted && !this.#sealed))
|
|
86
|
+
if (this.#closed && !(admitted && !this.#sealed)) {
|
|
87
|
+
record?.refusedByHost();
|
|
82
88
|
return Promise.reject(new HostStoppingError());
|
|
83
|
-
|
|
89
|
+
}
|
|
90
|
+
const inQueue = () => inChannel(channel, task);
|
|
91
|
+
return admitted ? this.#finishing.run(1, inQueue) : inQueue();
|
|
84
92
|
};
|
|
85
93
|
this.#sizes.set(channel, this.size(channel) + 1);
|
|
86
94
|
this.#changed(channel);
|
|
@@ -94,6 +102,7 @@ export class ChannelQueue {
|
|
|
94
102
|
else this.#sizes.delete(channel);
|
|
95
103
|
if (this.#tails.get(channel) === tail) this.#tails.delete(channel);
|
|
96
104
|
this.#changed(channel);
|
|
105
|
+
record?.end(channel);
|
|
97
106
|
});
|
|
98
107
|
return result;
|
|
99
108
|
}
|
|
@@ -15,12 +15,11 @@ import {
|
|
|
15
15
|
} from "../contract/channels.ts";
|
|
16
16
|
import type { SurfacePort } from "../contract/surface.ts";
|
|
17
17
|
import type { ChannelKey } from "../domain/sessions.ts";
|
|
18
|
-
import {
|
|
19
|
-
import type { Contact, ContactAssessor } from "../identity/contact.ts";
|
|
18
|
+
import type { ContactAssessor } from "../identity/contact.ts";
|
|
20
19
|
import type { IdentityService } from "../identity/identity-service.ts";
|
|
21
20
|
import { HostStoppingError, PluginError } from "../kernel/errors.ts";
|
|
22
21
|
import type { Logger } from "../kernel/log.ts";
|
|
23
|
-
import {
|
|
22
|
+
import { admitMessage } from "./admit-message.ts";
|
|
24
23
|
import { speakerOf } from "./background-speaker.ts";
|
|
25
24
|
import type { ChannelQueue } from "./channel-queue.ts";
|
|
26
25
|
import { tellDeleted, tellEdited } from "./claim-events.ts";
|
|
@@ -32,6 +31,8 @@ import {
|
|
|
32
31
|
QueuedMessages,
|
|
33
32
|
type RefuseTally,
|
|
34
33
|
} from "./queued-messages.ts";
|
|
34
|
+
import { tellStopping } from "./stopping-notice.ts";
|
|
35
|
+
import { RunningTurns, underRecord } from "./turn-records.ts";
|
|
35
36
|
|
|
36
37
|
export interface ChannelRouterOptions {
|
|
37
38
|
/** Claims in registration order; the router orders them by priority. */
|
|
@@ -55,6 +56,8 @@ export interface ChannelRouterOptions {
|
|
|
55
56
|
peerBots?: PeerBots;
|
|
56
57
|
/** The plugins' passive listeners; each message that reaches routing in an owned channel is handed to them. */
|
|
57
58
|
observers?: MessageObservers;
|
|
59
|
+
/** The runtime's stop, with which `abort` stops a turn in a channel whose claim has no `stop`. */
|
|
60
|
+
stopTurn?: (channel: ChannelKey) => boolean;
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
/** Claims by descending priority, keeping registration order among equals; refuses a repeated name. */
|
|
@@ -93,6 +96,8 @@ export class ChannelRouter implements ConversationPort {
|
|
|
93
96
|
readonly #warned = new Set<string>();
|
|
94
97
|
/** The channels already told that the host is restarting, so a burst of messages gets one notice. */
|
|
95
98
|
readonly #told = new Set<ChannelKey>();
|
|
99
|
+
/** The admitted turns running, so a shutdown that cuts one leaves its messages for the next process. */
|
|
100
|
+
readonly #turns = new RunningTurns();
|
|
96
101
|
|
|
97
102
|
constructor(options: ChannelRouterOptions) {
|
|
98
103
|
this.#options = options;
|
|
@@ -111,7 +116,10 @@ export class ChannelRouter implements ConversationPort {
|
|
|
111
116
|
* one turn.
|
|
112
117
|
*/
|
|
113
118
|
handle(message: InboundMessage): Promise<void> {
|
|
114
|
-
|
|
119
|
+
// Its own turn, never part of the one that delivered it, such as a plugin's turn passing it on.
|
|
120
|
+
return underRecord(undefined, () =>
|
|
121
|
+
this.#forwards.handle(message, (m) => this.#route(m)),
|
|
122
|
+
);
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
async #route(given: InboundMessage): Promise<void> {
|
|
@@ -151,7 +159,11 @@ export class ChannelRouter implements ConversationPort {
|
|
|
151
159
|
);
|
|
152
160
|
return undefined;
|
|
153
161
|
}
|
|
154
|
-
const admitted = await
|
|
162
|
+
const admitted = await admitMessage(claim, message, {
|
|
163
|
+
logger: this.#options.logger,
|
|
164
|
+
warned: this.#warned,
|
|
165
|
+
...(this.#options.contacts ? { contacts: this.#options.contacts } : {}),
|
|
166
|
+
});
|
|
155
167
|
if (!admitted) return undefined;
|
|
156
168
|
const { admission } = admitted;
|
|
157
169
|
if (listed) peers?.answered(message.channel);
|
|
@@ -178,70 +190,7 @@ export class ChannelRouter implements ConversationPort {
|
|
|
178
190
|
return run;
|
|
179
191
|
}
|
|
180
192
|
|
|
181
|
-
/**
|
|
182
|
-
* The claim's admission of the message, which carries the speaker its author resolves to; one
|
|
183
|
-
* who cannot be resolved, such as while the database is down, reaches the claim as no one. The
|
|
184
|
-
* author's contact is recorded, such as their first contact linked, only once the claim takes
|
|
185
|
-
* the message, so a message nobody serves links or makes no one. A linked author the rules
|
|
186
|
-
* refuse is still recorded as seen at no tier as the message is assessed, whether or not a
|
|
187
|
-
* claim then takes it, at most once in a while. When that record fails, or finds them someone
|
|
188
|
-
* else or no one now, such as linked elsewhere meanwhile, the message is dropped: it would run
|
|
189
|
-
* as a principal that may not exist, such as one only assessed at a first contact. The claim
|
|
190
|
-
* hears it through the admission's `dropped` or `unanswered`.
|
|
191
|
-
*/
|
|
192
|
-
async #admit(
|
|
193
|
-
claim: ChannelClaim,
|
|
194
|
-
message: InboundMessage,
|
|
195
|
-
): Promise<{ admission: Admission; message: InboundMessage } | undefined> {
|
|
196
|
-
const { logger, contacts } = this.#options;
|
|
197
|
-
const { speaker: _given, ...facts } = message;
|
|
198
|
-
const actor = actorOf(message, this.#warned, logger);
|
|
199
|
-
let contact: Contact | undefined;
|
|
200
|
-
if (actor && contacts)
|
|
201
|
-
try {
|
|
202
|
-
contact = await contacts.assess(actor, {
|
|
203
|
-
conversation: message.channel,
|
|
204
|
-
});
|
|
205
|
-
} catch (error) {
|
|
206
|
-
logger.error(
|
|
207
|
-
{ channel: message.channel, err: error },
|
|
208
|
-
"could not resolve who wrote a message; it reaches its claim as no one",
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
const admittedAs: InboundMessage = contact
|
|
212
|
-
? { ...facts, speaker: Object.freeze({ ...contact.speaker }) }
|
|
213
|
-
: facts;
|
|
214
|
-
const admission = claim.admit(admittedAs);
|
|
215
|
-
if (!admission) return undefined;
|
|
216
|
-
if (!contact) return { admission, message: admittedAs };
|
|
217
|
-
try {
|
|
218
|
-
const taken = await contact.take();
|
|
219
|
-
if (taken?.principalId === contact.speaker.principalId)
|
|
220
|
-
return { admission, message: admittedAs };
|
|
221
|
-
logger.warn(
|
|
222
|
-
{ channel: message.channel },
|
|
223
|
-
"the author of a message was linked elsewhere, or refused, while it was admitted; it does not run",
|
|
224
|
-
);
|
|
225
|
-
} catch (error) {
|
|
226
|
-
logger.error(
|
|
227
|
-
{ channel: message.channel, err: error },
|
|
228
|
-
"could not record who wrote a message; it does not run",
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
if (admission.kind === "turn") admission.dropped?.();
|
|
232
|
-
else
|
|
233
|
-
admission.unanswered({
|
|
234
|
-
status: "skipped",
|
|
235
|
-
reason: "its author was not who they were when it was admitted",
|
|
236
|
-
});
|
|
237
|
-
return undefined;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Drops a message the host no longer starts a turn for because it is shutting down, and tells
|
|
242
|
-
* its channel once that it restarts; no one is told for a bot's or an integration's message, such
|
|
243
|
-
* as a report, since a notice there would only start the next one.
|
|
244
|
-
*/
|
|
193
|
+
/** Drops a message the host no longer starts a turn for as it stops, and tells its channel (`tellStopping`). */
|
|
245
194
|
async #refuse(message: InboundMessage, admission: Admission): Promise<void> {
|
|
246
195
|
const { logger, surfaces } = this.#options;
|
|
247
196
|
const { channel } = message;
|
|
@@ -252,23 +201,7 @@ export class ChannelRouter implements ConversationPort {
|
|
|
252
201
|
reason: new HostStoppingError().message,
|
|
253
202
|
});
|
|
254
203
|
logger.info({ channel }, "message not run: the host is shutting down");
|
|
255
|
-
|
|
256
|
-
try {
|
|
257
|
-
// The surface may take it, and tell its channel itself.
|
|
258
|
-
if (await surfaces?.of(channel)?.refuseStopping?.(message)) return;
|
|
259
|
-
} catch (error) {
|
|
260
|
-
logger.warn(
|
|
261
|
-
{ channel, err: error },
|
|
262
|
-
"surface did not take a refused message",
|
|
263
|
-
);
|
|
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
|
-
);
|
|
204
|
+
await tellStopping(message, surfaces, this.#told, logger);
|
|
272
205
|
}
|
|
273
206
|
|
|
274
207
|
/**
|
|
@@ -289,21 +222,35 @@ export class ChannelRouter implements ConversationPort {
|
|
|
289
222
|
}
|
|
290
223
|
const { busy } = admission;
|
|
291
224
|
let waiting: QueuedEntry | undefined;
|
|
292
|
-
//
|
|
225
|
+
// During a turn a message joins it or is marked waiting; unmarkable, it is kept so a shutdown refuses it.
|
|
293
226
|
if (busy && queue.size(message.channel) > 0) {
|
|
294
|
-
|
|
227
|
+
// Joins the running turn's record first, so an answer that ends that turn as it takes the
|
|
228
|
+
// message counts for it; taken back when the turn does not take it.
|
|
229
|
+
const running = this.#turns.running(message.channel);
|
|
230
|
+
running?.add(message);
|
|
231
|
+
if (await busy.steer?.()) {
|
|
232
|
+
if (!running)
|
|
233
|
+
logger.warn(
|
|
234
|
+
{ channel: message.channel },
|
|
235
|
+
"message steered into a turn routing did not start; a shutdown that cuts that turn does not refuse it",
|
|
236
|
+
);
|
|
237
|
+
return { done: busy.react(STEERED_MARK) };
|
|
238
|
+
}
|
|
239
|
+
running?.remove(message);
|
|
295
240
|
// Listed before the mark is placed, so a delete or cancel during that wait reaches it.
|
|
296
241
|
waiting = this.#queued.add(claim, message, admission);
|
|
297
242
|
await busy.react(QUEUED_MARK);
|
|
298
243
|
if (waiting.cancelled) return { done: Promise.resolve() };
|
|
299
|
-
}
|
|
244
|
+
} else if (queue.size(message.channel) > 0)
|
|
245
|
+
waiting = this.#queued.add(claim, message, admission, false);
|
|
300
246
|
const done = queue
|
|
301
247
|
.run(message.channel, () => {
|
|
302
|
-
if (!waiting) return admission.run();
|
|
248
|
+
if (!waiting) return this.#turns.run(message, () => admission.run());
|
|
303
249
|
// From here no cancel or edit reaches it: the turn has started or it was cancelled before.
|
|
304
250
|
if (!this.#queued.remove(waiting)) return Promise.resolve();
|
|
305
251
|
this.#queued.unmark(waiting);
|
|
306
|
-
|
|
252
|
+
const { message: started, admission: admitted } = waiting;
|
|
253
|
+
return this.#turns.run(started, () => admitted.run());
|
|
307
254
|
})
|
|
308
255
|
// pi-lens-ignore: no-unknown-parameters
|
|
309
256
|
.catch(async (error: unknown) => {
|
|
@@ -324,6 +271,28 @@ export class ChannelRouter implements ConversationPort {
|
|
|
324
271
|
return { done };
|
|
325
272
|
}
|
|
326
273
|
|
|
274
|
+
/** Marks every admitted turn still running as cut, as the drain limit passes. */
|
|
275
|
+
cutRunning(): void {
|
|
276
|
+
this.#turns.cut();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The channels where turns of admitted messages run outside their queue, which the drain waits for too. */
|
|
280
|
+
busy(): ChannelKey[] {
|
|
281
|
+
return this.#turns.busy();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Leaves each person's message of every turn cut at the drain limit, or refused as the host
|
|
286
|
+
* stopped, that did not answer, through its surface as a refused one is, counting in `tally`;
|
|
287
|
+
* call it once the abort grace is over. `dropping` ends once what the turns handed on is dropped.
|
|
288
|
+
*/
|
|
289
|
+
leaveCut(tally: RefuseTally): Promise<{ dropping: Promise<unknown> }> {
|
|
290
|
+
const { logger, surfaces } = this.#options;
|
|
291
|
+
return this.#turns.leaveCut(tally, logger, (message) =>
|
|
292
|
+
tellStopping(message, surfaces, this.#told, logger),
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
327
296
|
/** Refuses each message still waiting behind a turn after the drain, counting in `tally`. */
|
|
328
297
|
refuseWaiting(tally: RefuseTally): Promise<void> {
|
|
329
298
|
return this.#queued.refuseAll(
|
|
@@ -359,7 +328,7 @@ export class ChannelRouter implements ConversationPort {
|
|
|
359
328
|
messageId: string,
|
|
360
329
|
space?: string,
|
|
361
330
|
): Promise<void> {
|
|
362
|
-
// The surface says it is gone: no author check,
|
|
331
|
+
// The surface says it is gone: no author check, no mark to remove; an unmarked one still runs, as ever.
|
|
363
332
|
this.#queued.cancel(channel, messageId, undefined, false);
|
|
364
333
|
return tellDeleted(
|
|
365
334
|
this.#owner(channel, space),
|
|
@@ -394,7 +363,12 @@ export class ChannelRouter implements ConversationPort {
|
|
|
394
363
|
* is checked to be; never rejects. A turn whose target no plugin contributes is skipped, never
|
|
395
364
|
* handed to another target's claim, and so is one whose author may not run it now.
|
|
396
365
|
*/
|
|
397
|
-
|
|
366
|
+
background(turn: BackgroundTurn): Promise<ScheduledOutcome> {
|
|
367
|
+
// Not part of a message's turn, even when work that turn started asks for it.
|
|
368
|
+
return underRecord(undefined, () => this.#background(turn));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async #background(turn: BackgroundTurn): Promise<ScheduledOutcome> {
|
|
398
372
|
if (this.#options.queue.closed) return stopping();
|
|
399
373
|
if (!this.#options.targets(turn.target))
|
|
400
374
|
return {
|
|
@@ -480,6 +454,18 @@ export class ChannelRouter implements ConversationPort {
|
|
|
480
454
|
return this.#owner(channel)?.stop?.(channel) ?? false;
|
|
481
455
|
}
|
|
482
456
|
|
|
457
|
+
/** Stops the channel's running turn as the host shuts down: its claim's `stop`, else the runtime's. */
|
|
458
|
+
abort(channel: ChannelKey): boolean {
|
|
459
|
+
const owner = this.#owner(channel);
|
|
460
|
+
if (owner?.stop) return owner.stop(channel);
|
|
461
|
+
const stopped = this.#options.stopTurn?.(channel) ?? false;
|
|
462
|
+
this.#options.logger.info(
|
|
463
|
+
{ channel, stopped },
|
|
464
|
+
"shutting down: asked the runtime to stop a turn whose claim has no stop",
|
|
465
|
+
);
|
|
466
|
+
return stopped;
|
|
467
|
+
}
|
|
468
|
+
|
|
483
469
|
postsInPlace(channel: ChannelKey): boolean {
|
|
484
470
|
return this.#owner(channel)?.postsInPlace === true;
|
|
485
471
|
}
|