pi-roundtable 0.52.1 → 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.
- package/CHANGELOG.md +13 -1
- package/README.md +1 -0
- package/docs/plugins.md +5 -5
- package/package.json +1 -1
- package/src/core/config/limits.ts +20 -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.ts +8 -6
- package/src/core/routing/channel-router.ts +4 -3
- package/src/core/routing/queued-messages.ts +27 -16
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,18 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
|
5
5
|
|
|
6
6
|
## [Unreleased]
|
|
7
7
|
|
|
8
|
+
## [0.52.2] - 2026-11-17
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- New config keys `abortGraceSeconds` (default 10, 1 to 600) and `refuseWaitingSeconds` (default 15, 1 to 600), and `drain.refuseWaitingMs` in `RoundtableOptions`: how long a shutdown gives the turns it stops at the drain limit to end, and the messages still waiting behind a turn to be refused. The service manager's kill timeout must be longer than `drainSeconds` + `abortGraceSeconds` + `refuseWaitingSeconds` plus a few seconds of teardown; under a 30-second kill, for example, 12 + 5 + 8 leaves five.
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- `discord.catchUp`: during the shutdown, a message addressed to the bot is promised an answer after the restart, and recorded as left, only when routing refuses it because the host stops, that is when a claim would have answered it. A message in a channel the bot does not serve, or one no claim admits, no longer gets a false "will answer this once back" notice.
|
|
17
|
+
- `discord.catchUp`: a message that arrived during the shutdown and could not be recorded as left no longer gets the "send it again" notice, which together with catch-up answering it from the window could answer it twice; it gets no notice and the next process's catch-up answers it.
|
|
18
|
+
- A message waiting behind a running turn is now refused when the shutdown drain ends whatever claim admitted it. Before, only a message whose claim marks waiting messages (a `busy` admission, as the core's own claims give) was refused then; one waiting behind a plugin claim's turn that ignored its abort was neither answered nor told. Such a message is not listed among the channel's queued messages, as before.
|
|
19
|
+
|
|
8
20
|
## [0.52.1] - 2026-11-17
|
|
9
21
|
|
|
10
22
|
### Fixed
|
|
@@ -14,7 +26,7 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
|
14
26
|
- `discord.catchUp`: a message the live connection delivered without a record, because the database was down at startup, is not caught up a second time by the same process.
|
|
15
27
|
- `discord.catchUp`: the inbound journal is pruned to 30 days every six hours while the host runs, not only at restart. The docs now say what happens when `catchUp` is turned off later.
|
|
16
28
|
- `discord.catchUp`: a mention that has a thread started from it, or a reaction from the bot, counts as answered. Before this fix, the first restart after catch-up was enabled re-delivered mentions answered in a thread, and the bot then posted a false "could not open a thread" notice.
|
|
17
|
-
- `discord.catchUp`: caught-up messages reach routing before the newer live messages in the same channel. A live message waits until catch-up has listed the channels it may read (
|
|
29
|
+
- `discord.catchUp`: caught-up messages reach routing before the newer live messages in the same channel. A live message waits until catch-up has listed the channels it may read (two database queries). In one of those channels, it also waits until catch-up is done.
|
|
18
30
|
|
|
19
31
|
## [0.52.0] - 2026-11-17
|
|
20
32
|
|
package/README.md
CHANGED
|
@@ -194,6 +194,7 @@ A turn works 10 minutes by default before it asks you whether to go on, and 30 m
|
|
|
194
194
|
| `autonomy.yolo.allow`, `autonomy.yolo.card` | none | Hold classes yolo mode may approve beyond its defaults (`shell.rm`, say), and rule names whose calls it approves although the rule reports no classes (`tool:repo_push`, say), and classes or rule names it never approves (`shell.push`, say). By default yolo approves a plain `git push` and writes outside the workspace and cards the other destructive shell classes; see the [plugin guide](docs/plugins.md#hold-classes-and-yolo-mode) |
|
|
195
195
|
| `autonomy.yolo.questionGraceSeconds` | `30` | In a yolo conversation, how long an `ask_user` card holds the turn, 5 to 600; unanswered, the turn goes on with its own best choice and the card stays open |
|
|
196
196
|
| `autonomy.yolo.timeoutCardSeconds` | `60` | In a yolo conversation, how long the timeout card waits, 10 to 3600; unanswered, the turn goes on as if you pressed Continue, up to `autonomy.longRunning.maxSeconds` |
|
|
197
|
+
| `drainSeconds`, `abortGraceSeconds`, `refuseWaitingSeconds` | `180`, `10`, `15` | On a shutdown, how long running turns get to finish, then how long the ones stopped at that limit get to end, then how long the messages still waiting behind a turn get to be refused; the service manager's stop or kill timeout should be longer than the three together, plus a few seconds for the teardown |
|
|
197
198
|
|
|
198
199
|
Without `discord`, or with `discord` but no `discord.guild`, the agents still run when `agents` lists them, in the rooms of a surface that hosts them, such as pi-roundtable-web; the host refuses to start when none does:
|
|
199
200
|
|
package/docs/plugins.md
CHANGED
|
@@ -2946,7 +2946,7 @@ A message counts when a person wrote it (not a bot, a webhook or Discord itself)
|
|
|
2946
2946
|
It is left alone when it is older than the window, when it was already taken in, or when it shows an answer: the bot or one of its own webhooks posted in that channel after it, a thread was started from it (by anyone, as the bot answers a mention in a thread), or the bot reacted to it.
|
|
2947
2947
|
The thread and the reaction matter at the first restart after `catchUp` is turned on, when nothing was recorded yet; a mention a person started a thread from is therefore not answered, since one missed answer is preferred to two.
|
|
2948
2948
|
Of what is left, the newest `maxMessages` (default 20, at most 100) go to routing oldest first.
|
|
2949
|
-
They go once every service has started; a live message that came after the connection became ready waits until catch-up has listed the channels it may read (
|
|
2949
|
+
They go once every service has started; a live message that came after the connection became ready waits until catch-up has listed the channels it may read (two database queries, before any request), and, in one of those, until catch-up is done, so the caught-up messages of a channel reach routing before its newer live ones.
|
|
2950
2950
|
Each one passes through the same claims, identity resolution and gates as a live message, with `InboundMessage.caughtUp` set, so a claim may say its answer comes late, for example that the assistant just restarted.
|
|
2951
2951
|
The log carries only counts and ids, never text.
|
|
2952
2952
|
|
|
@@ -2956,10 +2956,10 @@ Whichever records a message first delivers it; the other drops it, so a second r
|
|
|
2956
2956
|
A message that catch-up cannot record is skipped, and a crash after the record but before the turn leaves it unanswered: one missed answer is preferred to two.
|
|
2957
2957
|
The live connection still delivers a message it cannot record, such as while the database is down, and logs it; catch-up in the same process leaves it alone, so only a later restart that finds no answer to it in its channel could answer it again.
|
|
2958
2958
|
Catch-up reads the author of a server message as a member first; when that fails for any reason but the author having left the server, the message is skipped unrecorded, so a later restart may still answer it with the author's roles.
|
|
2959
|
-
Once the old process begins to shut down it runs no new turn, and an addressed message
|
|
2959
|
+
Once the old process begins to shut down it runs no new turn, and an addressed message that routing refuses for that reason, whether it arrives then or already waits behind a running turn, is recorded as left (a `left_at` time in the journal), and its channel is told once that the message will be answered after the restart. Routing decides first: a message no claim would answer, such as one in a channel the bot does not serve, is neither left nor promised an answer.
|
|
2960
2960
|
The next process answers each message left within the last day first, whatever was posted in its channel since and whatever `windowMinutes`, the page and `maxMessages` say, and takes it from the journal as it does, so it is answered once.
|
|
2961
|
-
A message still waiting behind a turn that outlives the drain is refused, and so left, once the drain ends and before the services stop, within 15 seconds for all of them; this covers
|
|
2962
|
-
A message that cannot be recorded as left gets the notice to send it again instead.
|
|
2961
|
+
A message still waiting behind a turn that outlives the drain is refused, and so left, once the drain ends and before the services stop, within 15 seconds for all of them (`refuseWaitingSeconds`); this covers every message waiting behind a turn, whether or not its claim's admission has a `busy` mark. A message another process already took in is left alone, with no notice.
|
|
2962
|
+
A message that cannot be recorded as left gets no notice when it arrived during the shutdown, since it has no record and the next catch-up finds it in its window; one recorded before gets the notice to send it again instead.
|
|
2963
2963
|
Any other person's message that routing refuses then tells its channel once to send it again, as without catch-up.
|
|
2964
2964
|
The Discord surface does this through the optional `ChatSurface.refuseStopping(message)`, which routing calls for a person's message it refuses while the host stops; a surface a plugin contributes may implement it too, and returns true when it told the channel itself, so routing posts no restart notice.
|
|
2965
2965
|
Each notice is recorded in the journal, so the next process does not take it for an answer; a notice that cannot be recorded is deleted again, and when catch-up cannot look the notices up it leaves that channel unread rather than guess.
|
|
@@ -4422,7 +4422,7 @@ The same host may call `run()` again after that.
|
|
|
4422
4422
|
On `SIGTERM` or `SIGINT` the bot takes no new work at once, finishes what runs, and stops serving last:
|
|
4423
4423
|
|
|
4424
4424
|
1. It stops starting work: the channel queue closes (`context.queue.closed` turns true), and from then on no new turn starts, whoever asks. A message that arrives is not run; its channel gets one short "restarting, try again shortly" reply, except a bot's or an integration's message, which is only logged. A background turn (a schedule's, a delegated task's report, an error report) is skipped, and so is a turn a plugin asks of `context.turns.run`, which rejects with `HostStoppingError`; a task for the queue that has not started, whether queued before or after, is refused with the same error. The scheduler claims nothing, so a due schedule stays due and fires after the next start if it is less than twelve hours late. A turn that is already running finishes. A plugin that starts turns from a timer or a route of its own should check `context.queue.closed` first.
|
|
4425
|
-
2. It waits until the channel queue is empty and no service reports `busy()` work, for at most three minutes by default (`drainSeconds` in the config, up to 14,400, that is four hours; `drain.limitMs` in `RoundtableOptions`). If work is left, it stops each running turn as the owner's stop does and calls `abort()` of each service that still has work, gives them ten seconds (`drain.abortGraceMs`) to end, and goes on; whatever is left is logged and given up on, and a turn still running in the queue after the grace is told to stop once more. Work that ends during the wait delivers its report through `context.queue.finishing`, which the queue admits until the wait is over; the queue is sealed after it.
|
|
4425
|
+
2. It waits until the channel queue is empty and no service reports `busy()` work, for at most three minutes by default (`drainSeconds` in the config, up to 14,400, that is four hours; `drain.limitMs` in `RoundtableOptions`). If work is left, it stops each running turn as the owner's stop does and calls `abort()` of each service that still has work, gives them ten seconds (`abortGraceSeconds` in the config, `drain.abortGraceMs`) to end, and goes on; whatever is left is logged and given up on, and a turn still running in the queue after the grace is told to stop once more. Work that ends during the wait delivers its report through `context.queue.finishing`, which the queue admits until the wait is over; the queue is sealed after it. Messages still waiting behind a turn, whatever claim admitted them, are then refused within 15 seconds (`refuseWaitingSeconds` in the config, `drain.refuseWaitingMs`) before the services stop. The service manager's stop or kill timeout must be longer than `drainSeconds` + `abortGraceSeconds` + `refuseWaitingSeconds` plus a few seconds for the teardown; a shorter one cuts the refusal and teardown off, and a message waiting behind a turn that ignores its abort is then neither answered nor told. Under a 30-second kill, for example, `drainSeconds: 12`, `abortGraceSeconds: 5` and `refuseWaitingSeconds: 8` leave five seconds.
|
|
4426
4426
|
3. Every plugin hears `shutdown(left)`, while every service is still running.
|
|
4427
4427
|
4. The HTTP listener closes, so no request reaches a service that has stopped.
|
|
4428
4428
|
5. Services stop in the reverse of the order they started.
|
package/package.json
CHANGED
|
@@ -7,8 +7,21 @@ import {
|
|
|
7
7
|
} from "../modes/autonomy.ts";
|
|
8
8
|
import { integer, list, optional, shape, text } from "./schema.ts";
|
|
9
9
|
|
|
10
|
+
/** How long work that was told to stop at the drain limit gets to end before the shutdown goes on. */
|
|
11
|
+
export const ABORT_GRACE_MS = 10_000;
|
|
12
|
+
/** How long the messages still waiting after the drain are given to be refused, before the teardown. */
|
|
13
|
+
export const REFUSE_WAITING_MS = 15_000;
|
|
14
|
+
|
|
10
15
|
/** The configuration's `turnLimits` and `autonomy`: how long a turn works, and what runs without a person. */
|
|
11
16
|
export interface LimitsConfig {
|
|
17
|
+
/**
|
|
18
|
+
* Seconds a shutdown gives the turns it stops at the drain limit to end; 1 to 600, default 10.
|
|
19
|
+
* A service manager's kill timeout should exceed `drainSeconds` + this + `refuseWaitingSeconds`,
|
|
20
|
+
* plus a few seconds for the teardown.
|
|
21
|
+
*/
|
|
22
|
+
abortGraceSeconds?: number;
|
|
23
|
+
/** Seconds a shutdown gives the refusal of the messages still waiting behind a turn; 1 to 600, default 15. */
|
|
24
|
+
refuseWaitingSeconds?: number;
|
|
12
25
|
/**
|
|
13
26
|
* How long one turn may work before it asks the owner on a card whether to go on, or is
|
|
14
27
|
* stopped when it cannot show a card. Time spent waiting on the owner's cards does not count.
|
|
@@ -51,8 +64,10 @@ export interface LimitsConfig {
|
|
|
51
64
|
};
|
|
52
65
|
}
|
|
53
66
|
|
|
54
|
-
/** The
|
|
67
|
+
/** The keys' shapes, spread into the configuration's. */
|
|
55
68
|
export const limitsShape = {
|
|
69
|
+
abortGraceSeconds: optional(integer(1, 600)),
|
|
70
|
+
refuseWaitingSeconds: optional(integer(1, 600)),
|
|
56
71
|
turnLimits: optional(
|
|
57
72
|
shape({
|
|
58
73
|
timeoutSeconds: optional(integer(60, 86_400)),
|
|
@@ -79,6 +94,8 @@ export const limitsShape = {
|
|
|
79
94
|
|
|
80
95
|
/** The limits with their defaults, in ms; `maxMs` is never below `timeoutMs`. */
|
|
81
96
|
export interface ResolvedLimits {
|
|
97
|
+
abortGraceMs: number;
|
|
98
|
+
refuseWaitingMs: number;
|
|
82
99
|
turnLimits: { timeoutMs: number; maxMs: number; cardMs: number };
|
|
83
100
|
autonomy: Autonomy;
|
|
84
101
|
}
|
|
@@ -94,6 +111,8 @@ export function limitsOf(config: LimitsConfig): ResolvedLimits {
|
|
|
94
111
|
);
|
|
95
112
|
const longRunning = autonomy?.longRunning?.maxSeconds;
|
|
96
113
|
return {
|
|
114
|
+
abortGraceMs: secondsOr(config.abortGraceSeconds, ABORT_GRACE_MS),
|
|
115
|
+
refuseWaitingMs: secondsOr(config.refuseWaitingSeconds, REFUSE_WAITING_MS),
|
|
97
116
|
turnLimits: {
|
|
98
117
|
timeoutMs: timeoutSeconds * 1_000,
|
|
99
118
|
maxMs: maxSeconds * 1_000,
|
|
@@ -49,9 +49,11 @@ const DEFAULT_RETRY_MS = 2000;
|
|
|
49
49
|
* Records each addressed message the live gateway delivers before routing hears it, so catch-up
|
|
50
50
|
* after a restart leaves it alone; false for one already recorded, which is not delivered again.
|
|
51
51
|
* A message that cannot be recorded is still delivered, logged, and kept in `unrecorded`.
|
|
52
|
-
* Once the host drains, an addressed message
|
|
53
|
-
*
|
|
54
|
-
*
|
|
52
|
+
* Once the host drains, an addressed message still reaches routing unrecorded, so only one routing
|
|
53
|
+
* refuses as the host stops, one a claim would have answered, is recorded as left: the next process
|
|
54
|
+
* answers it whatever its channel shows since, and its channel is told so once. One that cannot be
|
|
55
|
+
* left gets no notice, as catch-up finds it unrecorded in its window. Any other message routing
|
|
56
|
+
* refuses then tells its channel once to send it again. Each notice is recorded, so
|
|
55
57
|
* catch-up does not take it for an answer, and deleted again when it cannot be.
|
|
56
58
|
*/
|
|
57
59
|
export function journalGate({
|
|
@@ -65,6 +67,9 @@ export function journalGate({
|
|
|
65
67
|
redeliver,
|
|
66
68
|
}: GateOptions): JournalGate {
|
|
67
69
|
const told = { later: new Set<string>(), resend: new Set<string>() };
|
|
70
|
+
// Addressed messages passed during the drain without a record: routing decides whether they are left.
|
|
71
|
+
// An entry routing never refuses stays until the process exits; the drain bounds the set, and the process ends after it.
|
|
72
|
+
const unclaimed = new Set<string>();
|
|
68
73
|
const channelOf = (message: InboundMessage) =>
|
|
69
74
|
message.channel.slice("discord:".length);
|
|
70
75
|
const tell = async (
|
|
@@ -111,7 +116,8 @@ export function journalGate({
|
|
|
111
116
|
channelId: channelOf(message),
|
|
112
117
|
direct: message.isDirect,
|
|
113
118
|
});
|
|
114
|
-
// Left in the journal, the next process answers it whatever its channel shows
|
|
119
|
+
// Left in the journal, the next process answers it whatever its channel shows. A recorded one that
|
|
120
|
+
// cannot be left is told to be sent again; an unrecorded one is told nothing, as catch-up answers it.
|
|
115
121
|
const leave = async (
|
|
116
122
|
message: InboundMessage,
|
|
117
123
|
botId: string,
|
|
@@ -132,6 +138,13 @@ export function journalGate({
|
|
|
132
138
|
return;
|
|
133
139
|
}
|
|
134
140
|
} catch (error) {
|
|
141
|
+
if (!taken) {
|
|
142
|
+
logger.warn(
|
|
143
|
+
{ ...where, err: error },
|
|
144
|
+
"could not leave a message for the next process; unrecorded, catch-up answers it",
|
|
145
|
+
);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
135
148
|
logger.warn(
|
|
136
149
|
{ ...where, err: error },
|
|
137
150
|
"could not leave a message for the next process; its channel is told to send it again",
|
|
@@ -169,8 +182,9 @@ export function journalGate({
|
|
|
169
182
|
const pass = async (message: InboundMessage, botId: string) => {
|
|
170
183
|
if (!addressed(message)) return true;
|
|
171
184
|
if (draining()) {
|
|
172
|
-
|
|
173
|
-
|
|
185
|
+
// Routing refuses it, through `refused`, only when a claim would have answered it.
|
|
186
|
+
unclaimed.add(message.messageId);
|
|
187
|
+
return true;
|
|
174
188
|
}
|
|
175
189
|
const channelId = channelOf(message);
|
|
176
190
|
const { messageId } = message;
|
|
@@ -184,10 +198,8 @@ export function journalGate({
|
|
|
184
198
|
return false;
|
|
185
199
|
}
|
|
186
200
|
await wait?.(channelId);
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
await leave(message, botId);
|
|
190
|
-
return false;
|
|
201
|
+
// Should the drain have begun meanwhile, routing refuses it, and `refused` leaves it.
|
|
202
|
+
return true;
|
|
191
203
|
} catch (error) {
|
|
192
204
|
unrecorded?.add(messageId);
|
|
193
205
|
logger.warn(
|
|
@@ -199,7 +211,8 @@ export function journalGate({
|
|
|
199
211
|
};
|
|
200
212
|
const refused = async (message: InboundMessage, botId: string) => {
|
|
201
213
|
if (!notify || message.authorIsBot || message.integration) return false;
|
|
202
|
-
if (addressed(message))
|
|
214
|
+
if (addressed(message))
|
|
215
|
+
await leave(message, botId, !unclaimed.delete(message.messageId));
|
|
203
216
|
else await tell("resend", message, botId);
|
|
204
217
|
return true;
|
|
205
218
|
};
|
|
@@ -54,10 +54,6 @@ const migration: Migration = {
|
|
|
54
54
|
},
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
-
/**
|
|
58
|
-
* The journal over the host's pool, built from closures rather than private fields, so a test
|
|
59
|
-
* proxy can carry it. One insert that does nothing on a conflict decides who records a message.
|
|
60
|
-
*/
|
|
61
57
|
/** Marks a message left by a stopping process, so the next one answers it whatever the channel shows. */
|
|
62
58
|
const leftMigration: Migration = {
|
|
63
59
|
name: "discord-inbound-journal-left",
|
|
@@ -67,6 +63,10 @@ const leftMigration: Migration = {
|
|
|
67
63
|
},
|
|
68
64
|
};
|
|
69
65
|
|
|
66
|
+
/**
|
|
67
|
+
* The journal over the host's pool, built from closures rather than private fields, so a test
|
|
68
|
+
* proxy can carry it. One insert that does nothing on a conflict decides who records a message.
|
|
69
|
+
*/
|
|
70
70
|
export const PgInboundJournal = {
|
|
71
71
|
migrations: [migration, leftMigration],
|
|
72
72
|
attach(sql: SQL): InboundJournal {
|
|
@@ -360,7 +360,11 @@ export async function defineRoundtable(
|
|
|
360
360
|
],
|
|
361
361
|
judgeModel: judgeThrough(modelRuntime, config.judge.model),
|
|
362
362
|
apiKey: (provider) => registry.getApiKeyForProvider(provider),
|
|
363
|
-
drain: {
|
|
363
|
+
drain: {
|
|
364
|
+
limitMs: config.drainMs,
|
|
365
|
+
abortGraceMs: config.abortGraceMs,
|
|
366
|
+
refuseWaitingMs: config.refuseWaitingMs,
|
|
367
|
+
},
|
|
364
368
|
...(discord?.peerBots ? { peerBots: discord.peerBots } : {}),
|
|
365
369
|
...(overrides.aborted ? { aborted: overrides.aborted } : {}),
|
|
366
370
|
},
|
package/src/core/host/drain.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { DRAIN_LIMIT_MS } from "../config/config-schema.ts";
|
|
2
|
+
import { ABORT_GRACE_MS } from "../config/limits.ts";
|
|
2
3
|
import type { Logger } from "../kernel/log.ts";
|
|
3
4
|
import type { Service } from "../plugin-api/plugin.ts";
|
|
4
5
|
|
|
5
|
-
/** How long work that was told to stop gets to end before the shutdown goes on without it. */
|
|
6
|
-
const ABORT_GRACE_MS = 10_000;
|
|
7
|
-
|
|
8
6
|
export interface DrainOptions {
|
|
9
7
|
/** What still runs or waits, one entry per piece of work; empty when idle. */
|
|
10
8
|
busy: () => string[];
|
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;
|
|
@@ -326,7 +325,10 @@ export class Roundtable {
|
|
|
326
325
|
// Counted as it goes, and told to start no more once the limit passes.
|
|
327
326
|
const tally = { refused: 0, failed: 0, stopped: false };
|
|
328
327
|
const told = await attempt(() =>
|
|
329
|
-
withinMs(
|
|
328
|
+
withinMs(
|
|
329
|
+
drain?.refuseWaitingMs ?? REFUSE_WAITING_MS,
|
|
330
|
+
this.#link.router?.refuseWaiting(tally),
|
|
331
|
+
),
|
|
330
332
|
);
|
|
331
333
|
tally.stopped = true;
|
|
332
334
|
if (!told.ok || tally.failed > 0)
|
|
@@ -289,14 +289,15 @@ export class ChannelRouter implements ConversationPort {
|
|
|
289
289
|
}
|
|
290
290
|
const { busy } = admission;
|
|
291
291
|
let waiting: QueuedEntry | undefined;
|
|
292
|
-
//
|
|
292
|
+
// During a turn a message joins it or is marked waiting; unmarkable, it is kept so a shutdown refuses it.
|
|
293
293
|
if (busy && queue.size(message.channel) > 0) {
|
|
294
294
|
if (await busy.steer?.()) return { done: busy.react(STEERED_MARK) };
|
|
295
295
|
// Listed before the mark is placed, so a delete or cancel during that wait reaches it.
|
|
296
296
|
waiting = this.#queued.add(claim, message, admission);
|
|
297
297
|
await busy.react(QUEUED_MARK);
|
|
298
298
|
if (waiting.cancelled) return { done: Promise.resolve() };
|
|
299
|
-
}
|
|
299
|
+
} else if (queue.size(message.channel) > 0)
|
|
300
|
+
waiting = this.#queued.add(claim, message, admission, false);
|
|
300
301
|
const done = queue
|
|
301
302
|
.run(message.channel, () => {
|
|
302
303
|
if (!waiting) return admission.run();
|
|
@@ -359,7 +360,7 @@ export class ChannelRouter implements ConversationPort {
|
|
|
359
360
|
messageId: string,
|
|
360
361
|
space?: string,
|
|
361
362
|
): Promise<void> {
|
|
362
|
-
// The surface says it is gone: no author check,
|
|
363
|
+
// The surface says it is gone: no author check, no mark to remove; an unmarked one still runs, as ever.
|
|
363
364
|
this.#queued.cancel(channel, messageId, undefined, false);
|
|
364
365
|
return tellDeleted(
|
|
365
366
|
this.#owner(channel, space),
|
|
@@ -21,14 +21,10 @@ 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;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
/**
|
|
27
|
-
* The messages waiting for their own turn, by channel and message id: the router adds one when
|
|
28
|
-
* the running turn did not take it and takes it out when its turn starts, so a cancel or an edit
|
|
29
|
-
* can only reach a message that has not started. Everything is synchronous, so no cancel or edit
|
|
30
|
-
* races the start of a turn: whichever runs first wins.
|
|
31
|
-
*/
|
|
32
28
|
/** How a refusal of every waiting message went, filled in as it goes; set `stopped` to start no more. */
|
|
33
29
|
export interface RefuseTally {
|
|
34
30
|
refused: number;
|
|
@@ -36,6 +32,12 @@ export interface RefuseTally {
|
|
|
36
32
|
stopped: boolean;
|
|
37
33
|
}
|
|
38
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The messages waiting for their own turn, by channel and message id: the router adds one when
|
|
37
|
+
* the running turn did not take it and takes it out when its turn starts, so a cancel or an edit
|
|
38
|
+
* can only reach a message that has not started. Everything is synchronous, so no cancel or edit
|
|
39
|
+
* races the start of a turn: whichever runs first wins.
|
|
40
|
+
*/
|
|
39
41
|
export class QueuedMessages {
|
|
40
42
|
readonly #entries = new Map<ChannelKey, Map<string, QueuedEntry>>();
|
|
41
43
|
readonly #logger: Logger;
|
|
@@ -50,6 +52,7 @@ export class QueuedMessages {
|
|
|
50
52
|
claim: ChannelClaim,
|
|
51
53
|
message: InboundMessage,
|
|
52
54
|
admission: TurnAdmission,
|
|
55
|
+
listed = true,
|
|
53
56
|
): QueuedEntry {
|
|
54
57
|
const entry: QueuedEntry = {
|
|
55
58
|
claim,
|
|
@@ -57,6 +60,7 @@ export class QueuedMessages {
|
|
|
57
60
|
admission,
|
|
58
61
|
queuedAt: this.#now(),
|
|
59
62
|
cancelled: false,
|
|
63
|
+
listed,
|
|
60
64
|
};
|
|
61
65
|
const channel = this.#entries.get(message.channel) ?? new Map();
|
|
62
66
|
channel.set(message.messageId, entry);
|
|
@@ -75,14 +79,16 @@ export class QueuedMessages {
|
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
list(channel: ChannelKey): readonly QueuedMessage[] {
|
|
78
|
-
return [...(this.#entries.get(channel)?.values() ?? [])]
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
+
}));
|
|
86
92
|
}
|
|
87
93
|
|
|
88
94
|
/**
|
|
@@ -96,7 +102,7 @@ export class QueuedMessages {
|
|
|
96
102
|
by: QueuedBy | undefined,
|
|
97
103
|
unmark: boolean,
|
|
98
104
|
): QueuedChange {
|
|
99
|
-
const entry = this.#
|
|
105
|
+
const entry = this.#listed(channel, messageId);
|
|
100
106
|
if (!entry) return "unknown";
|
|
101
107
|
if (by && !this.#authored(entry, by)) return "forbidden";
|
|
102
108
|
this.remove(entry);
|
|
@@ -116,7 +122,7 @@ export class QueuedMessages {
|
|
|
116
122
|
by: QueuedBy | undefined,
|
|
117
123
|
text: string,
|
|
118
124
|
): QueuedChange {
|
|
119
|
-
const entry = this.#
|
|
125
|
+
const entry = this.#listed(channel, messageId);
|
|
120
126
|
if (!entry) return "unknown";
|
|
121
127
|
if (by && !this.#authored(entry, by)) return "forbidden";
|
|
122
128
|
if (entry.message.text === text) return "done";
|
|
@@ -177,6 +183,11 @@ export class QueuedMessages {
|
|
|
177
183
|
);
|
|
178
184
|
}
|
|
179
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
|
+
|
|
180
191
|
#authored(entry: QueuedEntry, by: QueuedBy): boolean {
|
|
181
192
|
const author = entry.message.speaker?.principalId;
|
|
182
193
|
return author !== undefined && author === by.principalId;
|