paseo-plugin-secure-input 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/OVERVIEW.md +28 -0
- package/README.md +105 -2
- package/SECURITY.md +84 -0
- package/assets/icon.png +0 -0
- package/client/card.tsx +74 -0
- package/client/codes.ts +32 -0
- package/client/diagnostics-rows.ts +139 -0
- package/client/dialog-state.ts +59 -0
- package/client/dialog.tsx +292 -0
- package/client/envelope-cache.ts +85 -0
- package/client/fields.tsx +255 -0
- package/client/file-entry.ts +142 -0
- package/client/form-state.ts +198 -0
- package/client/globals.d.ts +19 -0
- package/client/outcome.ts +96 -0
- package/client/presentation.ts +114 -0
- package/client/sealing.ts +27 -0
- package/client/self-test.ts +96 -0
- package/client/settings.tsx +160 -0
- package/client/submit.ts +92 -0
- package/client/text.ts +47 -0
- package/client/tone.ts +36 -0
- package/client/web.ts +184 -0
- package/index.client.tsx +29 -0
- package/index.server.ts +6 -0
- package/npm-shrinkwrap.json +60 -0
- package/package.json +68 -4
- package/paseo-plugin.json +6 -0
- package/server/core/continuation.ts +151 -0
- package/server/core/entries.ts +89 -0
- package/server/core/outbox.ts +555 -0
- package/server/core/registry-store.ts +185 -0
- package/server/core/registry.ts +1031 -0
- package/server/core/selftest.ts +60 -0
- package/server/core/view.ts +38 -0
- package/server/io/atomic.ts +119 -0
- package/server/io/deadline.ts +28 -0
- package/server/io/descriptor.ts +36 -0
- package/server/io/directory.ts +40 -0
- package/server/io/drop.ts +434 -0
- package/server/io/legacy-shim.ts +108 -0
- package/server/io/ownership.ts +8 -0
- package/server/io/private-file.ts +86 -0
- package/server/paseo/bounded.ts +13 -0
- package/server/paseo/handlers.ts +124 -0
- package/server/paseo/hooks.ts +106 -0
- package/server/paseo/plugin.ts +341 -0
- package/server/paseo/runtime.ts +409 -0
- package/server/paseo/tools.ts +178 -0
- package/shared/bytes.ts +100 -0
- package/shared/canonical.ts +32 -0
- package/shared/card.ts +41 -0
- package/shared/contracts.ts +128 -0
- package/shared/errors.ts +48 -0
- package/shared/limits.ts +118 -0
- package/shared/request-id.ts +9 -0
- package/shared/request.ts +66 -0
- package/shared/sanitize.ts +101 -0
- package/shared/seal.ts +292 -0
- package/shared/shown.ts +12 -0
- package/shared/text.ts +39 -0
- package/shared/view.ts +42 -0
- package/skills/secure-input/SKILL.md +150 -0
|
@@ -0,0 +1,1031 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { toBase64Url, utf8 } from "../../shared/bytes";
|
|
4
|
+
import { canonicalJson } from "../../shared/canonical";
|
|
5
|
+
import { SecureInputError, errorDetailOf, type ErrorCode } from "../../shared/errors";
|
|
6
|
+
import { DETAIL_MAX, DROP_TTL_MS } from "../../shared/limits";
|
|
7
|
+
import { FULFILLED_STATES, InputRequestSchema, LIMITS, type InputRequest, type RequestState } from "../../shared/request";
|
|
8
|
+
import { sanitizeText } from "../../shared/sanitize";
|
|
9
|
+
import { buildAad, decodeValues, digestHex, generateRequestKeyPair, open as unseal, type PayloadEntry, type SealedEnvelope } from "../../shared/seal";
|
|
10
|
+
import { RequestViewSchema } from "../../shared/view";
|
|
11
|
+
import { raceDeadline } from "../io/deadline";
|
|
12
|
+
import { setName, type DropFile, type DropStore, type SweepResult } from "../io/drop";
|
|
13
|
+
import { validateEntries } from "./entries";
|
|
14
|
+
import {
|
|
15
|
+
HISTORY,
|
|
16
|
+
MAX_REQUESTS_BYTES,
|
|
17
|
+
MAX_RETRIES,
|
|
18
|
+
TITLE_MAX,
|
|
19
|
+
encodeRecords,
|
|
20
|
+
isOpen,
|
|
21
|
+
parseRecord,
|
|
22
|
+
readStored,
|
|
23
|
+
saveStored,
|
|
24
|
+
type ReleasedBy,
|
|
25
|
+
type RequestRecord,
|
|
26
|
+
} from "./registry-store";
|
|
27
|
+
import { composeView } from "./view";
|
|
28
|
+
|
|
29
|
+
// Declared in `registry-store` (the number of failed submissions in `shared/limits`, where the app reads it too) and
|
|
30
|
+
// exported here, where what uses the registry looks for them.
|
|
31
|
+
export { HISTORY, MAX_REQUESTS_BYTES, MAX_RETRIES };
|
|
32
|
+
export type { RequestRecord };
|
|
33
|
+
|
|
34
|
+
export const TTL_MS = 8 * 60 * 60 * 1000;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* How long `shutdown` waits at most for the work queue, in which the sets are released and the last save is made. What
|
|
38
|
+
* has not ended by then is left: see `shutdown`. It and `DROP_SWEEP_BOUND_MS` after it are what the stop of the plugin
|
|
39
|
+
* spends on `drop/`, and the two are chosen to end well inside the 2 seconds Paseo 0.14 gives a plugin's process to
|
|
40
|
+
* stop before it sends SIGTERM (`PASEO_STOP_WINDOW_MS` in `server/paseo/plugin.ts`, which cites the daemon's lines).
|
|
41
|
+
* Honest work in the queue (a set being written, a save) takes milliseconds; a task that is still running after a
|
|
42
|
+
* second is a disk that stalls, and the sweep does not wait for it any longer.
|
|
43
|
+
*/
|
|
44
|
+
export const SHUTDOWN_BOUND_MS = 1_000;
|
|
45
|
+
|
|
46
|
+
/** How long `shutdown` gives the sweep of `drop/` that follows, at most: a few sets of at most 32 files take milliseconds. */
|
|
47
|
+
export const DROP_SWEEP_BOUND_MS = 500;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* How long the operation of a tool call waits at most for its turn in the work queue (`admit`). Honest work in the queue
|
|
51
|
+
* takes milliseconds; an operation still waiting after this is behind a disk that stalls, and its call is refused in
|
|
52
|
+
* fixed words (`BUSY`) instead of being held until Paseo's own limit on a tool call, ten minutes. An operation that has
|
|
53
|
+
* begun is not cut off by it.
|
|
54
|
+
*/
|
|
55
|
+
export const ADMISSION_WAIT_MS = 10_000;
|
|
56
|
+
|
|
57
|
+
/** What a tool call is refused with when its operation's turn has not come within `ADMISSION_WAIT_MS`: nothing of it was done, and nothing will be. */
|
|
58
|
+
export const BUSY = "the plugin is busy; retry in a few seconds";
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* What a tool call that was canceled while its operation waited for its turn is answered with: nothing of it was done,
|
|
62
|
+
* and nothing will be. Paseo drops what a canceled call answers, so no agent reads it; it is fixed all the same.
|
|
63
|
+
*/
|
|
64
|
+
export const CANCELED = "the call was canceled";
|
|
65
|
+
|
|
66
|
+
/** The parts of the drop store the registry uses. */
|
|
67
|
+
export type DropPort = Pick<DropStore, "hold" | "write" | "exists" | "release" | "remove" | "sweep">;
|
|
68
|
+
|
|
69
|
+
const NEXT = "End your turn now. You will receive a message when the user has submitted. Do not poll.";
|
|
70
|
+
const REQUESTS_FILE = "requests.json";
|
|
71
|
+
/** What a request is refused with when the dialog could not be given its view. The part of the view that does not fit is named after it. */
|
|
72
|
+
const NO_VIEW = "the request cannot be shown in the dialog";
|
|
73
|
+
/** The most of a path into the view that a refusal names. */
|
|
74
|
+
const VIEW_PATH_MAX = 80;
|
|
75
|
+
const SHUTTING_DOWN = "the plugin is shutting down";
|
|
76
|
+
|
|
77
|
+
export interface AgentIdentity {
|
|
78
|
+
agentId: string;
|
|
79
|
+
cwd: string;
|
|
80
|
+
title: string | null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** `detail` is what the module that refused put in its error: key names, an errno name, a fixed phrase. Never a value. */
|
|
84
|
+
export type FulfillResult = { ok: true; state: RequestState } | { ok: false; code: ErrorCode; retryable: boolean; detail?: string };
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* What a change of a record was, for whoever is told of it. The state changed (a request that was just made counts: it
|
|
88
|
+
* came to be `pending`), or the tamper flag was raised, or something else changed (a failed attempt was counted). A
|
|
89
|
+
* closed set, and each change is one of them: whoever is told can announce a state once, when it is reached, and not
|
|
90
|
+
* again each time something else about the record changes.
|
|
91
|
+
*/
|
|
92
|
+
export type RecordChange = "state" | "tamper" | "other";
|
|
93
|
+
|
|
94
|
+
export interface RegistryDeps {
|
|
95
|
+
dataDir: string;
|
|
96
|
+
/** Where the sets are written and from where they are removed. */
|
|
97
|
+
drop: DropPort;
|
|
98
|
+
/**
|
|
99
|
+
* Told of every change of a record, with a copy of it and what the change was. A record that is loaded as it was
|
|
100
|
+
* saved is no change and is not told; the changes the load makes itself (`pending` to `expired`, `delivered` to
|
|
101
|
+
* `released`) are changes of state.
|
|
102
|
+
*/
|
|
103
|
+
onChange(record: RequestRecord, change: RecordChange): void;
|
|
104
|
+
/** Told the id of a record that left the history, so that what others keep per request can go with it. */
|
|
105
|
+
onForget?(requestId: string): void;
|
|
106
|
+
now(): number;
|
|
107
|
+
/** The most `requests.json` may hold. Absent, `MAX_REQUESTS_BYTES`; a test sets it small. */
|
|
108
|
+
maxStoredBytes?: number;
|
|
109
|
+
/** `SHUTDOWN_BOUND_MS` unless a test wants less. */
|
|
110
|
+
shutdownBoundMs?: number;
|
|
111
|
+
/** `ADMISSION_WAIT_MS` unless a test wants less. */
|
|
112
|
+
admissionWaitMs?: number;
|
|
113
|
+
/** How long a set stays at the most, and the delay of its timer: `DROP_TTL_MS` unless a test wants less. */
|
|
114
|
+
dropTtlMs?: number;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Where a delivered request is on the way to the end of the turn that takes its set: its message was handed over to the
|
|
119
|
+
* agent (`sent`), and then a turn of that agent began (`armed`). The end of an armed request's agent's turn releases it.
|
|
120
|
+
*/
|
|
121
|
+
type TurnMark = "sent" | "armed";
|
|
122
|
+
|
|
123
|
+
/** What a tool call asks an operation with (`admit`): the call's signal, which says when the call was canceled. */
|
|
124
|
+
export interface CallTerms {
|
|
125
|
+
signal: AbortSignal;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const UNKNOWN_REQUEST = "unknown request";
|
|
129
|
+
const NO_FILES = "the request has no files to release";
|
|
130
|
+
const NOT_REMOVED = "the files could not be deleted; the plugin tries again";
|
|
131
|
+
|
|
132
|
+
/** Thrown where a write meets a shutdown that began meanwhile, right before its effect. Not a refusal of the request: nothing about the request changes. */
|
|
133
|
+
class ShutdownBegan extends Error {}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The digest of what a request asked for, which its submission is sealed to (the AAD, `buildAad`): its declared keys,
|
|
137
|
+
* in their order, and whether each is optional. The reason, the labels, the kind and the format are not in it: the
|
|
138
|
+
* kind is held to the request by the daemon itself (`validateEntries`), the format by the app.
|
|
139
|
+
*/
|
|
140
|
+
export function requestDigestOf(request: InputRequest): string {
|
|
141
|
+
return digestHex(utf8.encode(canonicalJson({ declared: request.fields.map((field) => ({ key: field.key, optional: field.optional === true })) })));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** What an earlier module's error says besides its code (key names, an errno name, a fixed phrase), cleaned and capped. Nothing for an error that is not the plugin's own. */
|
|
145
|
+
function detailOf(error: unknown): string | undefined {
|
|
146
|
+
return errorDetailOf(error, DETAIL_MAX);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function refusal(code: ErrorCode, retryable: boolean, detail?: string): FulfillResult {
|
|
150
|
+
return detail === undefined ? { ok: false, code, retryable } : { ok: false, code, retryable, detail };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The text the human reads in a request is stored clean, so a record that is saved is a record that loads. */
|
|
154
|
+
function cleanRequest(request: InputRequest): InputRequest {
|
|
155
|
+
const fields = request.fields.map(({ label, ...field }) => {
|
|
156
|
+
const text = label === undefined ? "" : sanitizeText(label, LIMITS.label);
|
|
157
|
+
return text === "" ? field : { ...field, label: text };
|
|
158
|
+
});
|
|
159
|
+
return { ...request, reason: sanitizeText(request.reason, LIMITS.reason), fields };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** What `fulfill` takes from the app, checked for type before anything is looked up: the RPC layer is not the only caller. */
|
|
163
|
+
function isWellFormed(id: unknown, envelope: unknown): envelope is SealedEnvelope {
|
|
164
|
+
if (typeof id !== "string") return false;
|
|
165
|
+
if (typeof envelope !== "object" || envelope === null) return false;
|
|
166
|
+
const { v, epk, nonce, ct } = envelope as Record<string, unknown>;
|
|
167
|
+
return typeof v === "number" && typeof epk === "string" && typeof nonce === "string" && typeof ct === "string";
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const copyOf = <T>(value: T): T => structuredClone(value);
|
|
171
|
+
|
|
172
|
+
export class Registry {
|
|
173
|
+
private readonly records = new Map<string, RequestRecord>();
|
|
174
|
+
private readonly keys = new Map<string, Uint8Array>();
|
|
175
|
+
private queue: Promise<unknown> = Promise.resolve();
|
|
176
|
+
private persistence: "ok" | "failing" = "ok";
|
|
177
|
+
/** The last sweep of `drop/` could not run at all (it was refused before it read the root): nothing is known of what is there. */
|
|
178
|
+
private sweepFailed = false;
|
|
179
|
+
/** Requests whose set could not be removed when it was released: they stay delivered, and are tried again. */
|
|
180
|
+
private readonly stuck = new Set<string>();
|
|
181
|
+
/**
|
|
182
|
+
* What `drop/` still owes: entries that were to go and are still there, by their names in `drop/`, each tried again by
|
|
183
|
+
* itself at the minute tick until it is gone (`settleOwed`), never by a sweep, which would take the sets that are live
|
|
184
|
+
* too. They come from a sweep that could not remove them (`noteSwept`) and from a write whose clean-up could not take
|
|
185
|
+
* away what it staged (`deliver`). The set of a request that was restored delivered and that the start's sweep could
|
|
186
|
+
* not remove names that request (`load`): the restart was to release it, and the request stays delivered until the set
|
|
187
|
+
* is gone. Anything else (a leftover, a stray) names nothing. Memory only: a start sweeps everything anyway.
|
|
188
|
+
*/
|
|
189
|
+
private readonly owed = new Map<string, string | null>();
|
|
190
|
+
/** The timer of each delivered set's deadline (`schedule`). Memory only: a restart sweeps every set anyway. */
|
|
191
|
+
private readonly timers = new Map<string, NodeJS.Timeout>();
|
|
192
|
+
/** How far each delivered request is on the way to the end of its turn (`TurnMark`). Memory only, as the timers. */
|
|
193
|
+
private readonly turns = new Map<string, TurnMark>();
|
|
194
|
+
private stored: "ok" | "unreadable" = "ok";
|
|
195
|
+
private dropped = 0;
|
|
196
|
+
/** Set when shutdown begins, or by `stop` ahead of it: nothing new starts. */
|
|
197
|
+
private stopped = false;
|
|
198
|
+
/** Set when the stop's sweep of `drop/` begins: from then on a set that is not there may be the sweep's doing (`takenBy`). */
|
|
199
|
+
private sweeping = false;
|
|
200
|
+
/** Set when shutdown is done: this instance starts no save and notifies nobody from then on. A save that was under way lands (see `shutdown`). */
|
|
201
|
+
private finished = false;
|
|
202
|
+
private ending: Promise<void> | null = null;
|
|
203
|
+
private swept: Promise<void> | null = null;
|
|
204
|
+
/** Requests that left the history and that nobody was told of yet. */
|
|
205
|
+
private readonly left: string[] = [];
|
|
206
|
+
|
|
207
|
+
constructor(private readonly deps: RegistryDeps) {}
|
|
208
|
+
|
|
209
|
+
/** One operation at a time: this is what makes "first submission wins" true under concurrent calls. */
|
|
210
|
+
private serial<T>(task: () => Promise<T>): Promise<T> {
|
|
211
|
+
const next = this.queue.catch(() => undefined).then(task);
|
|
212
|
+
this.queue = next;
|
|
213
|
+
return next;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The operation of a tool call, in the queue (`serial`), on the call's terms. It waits for its turn at most
|
|
218
|
+
* `ADMISSION_WAIT_MS`, and no longer than the call does: once the call's signal says it was canceled, it waits no more.
|
|
219
|
+
* A call that stops waiting is refused at once in fixed words (`BUSY`, `CANCELED`), and when the turn comes its
|
|
220
|
+
* operation is skipped: nothing of it happens, then or later (no key, no record, no card, no release). An operation
|
|
221
|
+
* whose turn came while its call still waited has begun, and is waited for to the end, whatever the signal or the clock
|
|
222
|
+
* say from then on: an effect that has begun is never left to run with nobody waiting for its outcome, and that outcome
|
|
223
|
+
* is the call's answer. Without terms an operation is queued as any other.
|
|
224
|
+
*/
|
|
225
|
+
private admit<T>(call: CallTerms | undefined, task: () => Promise<T>): Promise<T> {
|
|
226
|
+
if (call === undefined) return this.serial(task);
|
|
227
|
+
const { signal } = call;
|
|
228
|
+
if (signal.aborted) return Promise.reject(new SecureInputError("service_unavailable", CANCELED));
|
|
229
|
+
return new Promise<T>((resolve, reject) => {
|
|
230
|
+
let waiting = true;
|
|
231
|
+
const stopWaiting = (): void => {
|
|
232
|
+
waiting = false;
|
|
233
|
+
clearTimeout(timer);
|
|
234
|
+
signal.removeEventListener("abort", canceled);
|
|
235
|
+
};
|
|
236
|
+
const giveUp = (why: string) => (): void => {
|
|
237
|
+
if (!waiting) return;
|
|
238
|
+
stopWaiting();
|
|
239
|
+
reject(new SecureInputError("service_unavailable", why));
|
|
240
|
+
};
|
|
241
|
+
const canceled = giveUp(CANCELED);
|
|
242
|
+
const timer = setTimeout(giveUp(BUSY), this.deps.admissionWaitMs ?? ADMISSION_WAIT_MS);
|
|
243
|
+
timer.unref();
|
|
244
|
+
signal.addEventListener("abort", canceled, { once: true });
|
|
245
|
+
void this.serial(async () => {
|
|
246
|
+
// The call stopped waiting before the turn came: the operation is not begun.
|
|
247
|
+
if (!waiting) return;
|
|
248
|
+
stopWaiting();
|
|
249
|
+
try {
|
|
250
|
+
resolve(await task());
|
|
251
|
+
} catch (error) {
|
|
252
|
+
reject(error);
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* For the diagnostics: whether state is being saved, whether everything in `drop/` that was to go went (the last sweep
|
|
260
|
+
* ran, every set that was released went, and nothing is owed), whether `requests.json` could be read at start, and how
|
|
261
|
+
* many of its entries were not loaded.
|
|
262
|
+
*/
|
|
263
|
+
health(): { persistence: "ok" | "failing"; drop: "ok" | "failing"; stored: "ok" | "unreadable"; dropped: number } {
|
|
264
|
+
const drop = this.sweepFailed || this.stuck.size > 0 || this.owed.size > 0 ? "failing" : "ok";
|
|
265
|
+
return { persistence: this.persistence, drop, stored: this.stored, dropped: this.dropped };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* For the diagnostics: how many requests wait for the human, how many sets are delivered (their files are on the daemon,
|
|
270
|
+
* whether or not one could not be removed), and the nearest deadline among those sets, or null when there is none. Read
|
|
271
|
+
* from memory, like `health`.
|
|
272
|
+
*/
|
|
273
|
+
counts(): { pending: number; delivered: number; nextDeleteBy: number | null } {
|
|
274
|
+
let pending = 0;
|
|
275
|
+
let delivered = 0;
|
|
276
|
+
let nextDeleteBy: number | null = null;
|
|
277
|
+
for (const record of this.records.values()) {
|
|
278
|
+
if (record.state === "pending") pending += 1;
|
|
279
|
+
else if (record.state === "delivered") {
|
|
280
|
+
delivered += 1;
|
|
281
|
+
if (record.deleteBy !== null && (nextDeleteBy === null || record.deleteBy < nextDeleteBy)) nextDeleteBy = record.deleteBy;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return { pending, delivered, nextDeleteBy };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Closed records beyond HISTORY go; an open record never does. The oldest is the one closed longest ago: by creation
|
|
289
|
+
* time, a request that waited while HISTORY newer ones came and went would be dropped by the commit that closes it.
|
|
290
|
+
*/
|
|
291
|
+
private trim(): void {
|
|
292
|
+
const closed = [...this.records.values()].filter((record) => !isOpen(record));
|
|
293
|
+
closed.sort((a, b) => a.updatedAt - b.updatedAt);
|
|
294
|
+
for (const record of closed.slice(0, Math.max(0, closed.length - HISTORY))) this.forget(record.id);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private forget(id: string): void {
|
|
298
|
+
this.records.delete(id);
|
|
299
|
+
this.left.push(id);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Nothing is kept any more for the lifetime of a request's set: its timer is cleared, and its turn mark goes. */
|
|
303
|
+
private forgetLifetime(id: string): void {
|
|
304
|
+
const timer = this.timers.get(id);
|
|
305
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
306
|
+
this.timers.delete(id);
|
|
307
|
+
this.turns.delete(id);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* The deadline of a set that was just written, by a timer of its own, so that the 15 minutes are kept when nothing
|
|
312
|
+
* else happens; the minute tick (`expire`) compares the wall clock with the deadline for a timer that did not fire on
|
|
313
|
+
* time (a machine that slept). When it fires, the set is released as `time` in the queue, if the request is still
|
|
314
|
+
* delivered and shutdown has not begun. Unref'd, so that it keeps no process alive; cleared when the set goes another
|
|
315
|
+
* way, and at shutdown.
|
|
316
|
+
*/
|
|
317
|
+
private schedule(id: string): void {
|
|
318
|
+
const timer = setTimeout(() => {
|
|
319
|
+
this.timers.delete(id);
|
|
320
|
+
this.serial(async () => {
|
|
321
|
+
const record = this.records.get(id);
|
|
322
|
+
if (!this.stopped && record?.state === "delivered") await this.releaseSet(record, "time");
|
|
323
|
+
}).catch(() => undefined);
|
|
324
|
+
}, this.deps.dropTtlMs ?? DROP_TTL_MS);
|
|
325
|
+
timer.unref();
|
|
326
|
+
this.timers.set(id, timer);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Tells of the requests that left the history since this was last done. It is done at each change, after the change
|
|
331
|
+
* has been told and not before: a request can leave with the very change that closes it (when the open ones leave
|
|
332
|
+
* no room for it), and whoever keeps something for a request must hear "it changed" and then "it is gone", or
|
|
333
|
+
* would keep it for good.
|
|
334
|
+
*/
|
|
335
|
+
private tellForgotten(): void {
|
|
336
|
+
for (const id of this.left.splice(0)) {
|
|
337
|
+
try {
|
|
338
|
+
this.deps.onForget?.(id);
|
|
339
|
+
} catch {
|
|
340
|
+
// What a consumer does with it never changes what happened.
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Memory is the truth: a file that cannot be written is remembered (`health`), tried again at the next change, and decides nothing. */
|
|
346
|
+
private async persist(): Promise<void> {
|
|
347
|
+
if (this.finished) return;
|
|
348
|
+
try {
|
|
349
|
+
this.trim();
|
|
350
|
+
const { text, dropped } = encodeRecords([...this.records.values()], this.deps.maxStoredBytes);
|
|
351
|
+
for (const record of dropped) this.forget(record.id);
|
|
352
|
+
if (text === null) {
|
|
353
|
+
// Open records alone do not fit. A file the reader would refuse is not written.
|
|
354
|
+
this.persistence = "failing";
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
await saveStored(join(this.deps.dataDir, REQUESTS_FILE), text);
|
|
358
|
+
this.persistence = "ok";
|
|
359
|
+
} catch {
|
|
360
|
+
this.persistence = "failing";
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private notify(record: RequestRecord, change: RecordChange): void {
|
|
365
|
+
// A save that was under way when shutdown ended comes back here: nobody is told by an instance that is done.
|
|
366
|
+
if (this.finished) return;
|
|
367
|
+
try {
|
|
368
|
+
const result: unknown = this.deps.onChange(copyOf(record), change);
|
|
369
|
+
// An async consumer hands back a promise, and its rejection is no more ours to crash on than a throw.
|
|
370
|
+
if (typeof (result as PromiseLike<unknown> | undefined)?.then === "function") {
|
|
371
|
+
(result as PromiseLike<unknown>).then(undefined, () => undefined);
|
|
372
|
+
}
|
|
373
|
+
} catch {
|
|
374
|
+
// What a consumer does with a change never changes what happened.
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
private async commit(record: RequestRecord, change: RecordChange): Promise<void> {
|
|
379
|
+
record.updatedAt = this.deps.now();
|
|
380
|
+
if (this.finished) return;
|
|
381
|
+
await this.persist();
|
|
382
|
+
this.notify(record, change);
|
|
383
|
+
this.tellForgotten();
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private dropKey(id: string): void {
|
|
387
|
+
this.keys.get(id)?.fill(0);
|
|
388
|
+
this.keys.delete(id);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private require(id: string): RequestRecord {
|
|
392
|
+
const record = this.records.get(id);
|
|
393
|
+
if (!record) throw new SecureInputError("invalid_request", "unknown request");
|
|
394
|
+
return record;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* What a sweep of `drop/` that ran left, by name. What it took away is owed no more, and a request whose set it took is
|
|
399
|
+
* stuck no more. What it left is owed (`owed`), unless it is the set of a delivered request, which that request owes
|
|
400
|
+
* itself (`stuck`, or `owed` under its id): so a leftover the sweep could not remove is tried again at the tick, by
|
|
401
|
+
* itself. The set an owed request names stays owed until its request is released, which the tick does.
|
|
402
|
+
*/
|
|
403
|
+
private noteSwept({ left }: SweepResult): void {
|
|
404
|
+
this.sweepFailed = false;
|
|
405
|
+
const still = new Set(left);
|
|
406
|
+
for (const [name, requestId] of [...this.owed]) if (requestId === null && !still.has(name)) this.owed.delete(name);
|
|
407
|
+
for (const id of [...this.stuck]) if (!still.has(setName(id))) this.stuck.delete(id);
|
|
408
|
+
const delivered = new Set([...this.records.values()].filter((record) => record.state === "delivered").map((record) => setName(record.id)));
|
|
409
|
+
for (const name of left) if (!this.owed.has(name) && !delivered.has(name)) this.owed.set(name, null);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** Whether the restart owes this request's set: it was restored delivered, and the start's sweep could not remove it (`owed`). */
|
|
413
|
+
private owes(record: RequestRecord): boolean {
|
|
414
|
+
return this.owed.get(setName(record.id)) === record.id;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Removes what a run that was killed left behind: everything in `drop/`. Once per instance, and before anything else
|
|
419
|
+
* of a start but holding the data directory: a set with values in it must not outlive a start that ends in a failure
|
|
420
|
+
* further on, and the sweep needs nothing but the directory. `drop/` is held first, and one that is no private
|
|
421
|
+
* directory of this user fails the start-up, with nothing removed behind it. What the sweep cannot remove is left and
|
|
422
|
+
* reported (`health`), and owed (`noteSwept`): the tick tries each such entry again by itself until it is gone.
|
|
423
|
+
*
|
|
424
|
+
* A registry that is stopped sweeps nothing. A sweep that gets its turn after the stop does not begin, and one that
|
|
425
|
+
* is under way asks again right before each removal (`stillWanted`): the plugin it belongs to is no longer waited for,
|
|
426
|
+
* and what is in `drop/` by then may be the sets of the plugin started after it. So a sweep the stop overtook owes
|
|
427
|
+
* nothing either.
|
|
428
|
+
*/
|
|
429
|
+
sweep(): Promise<void> {
|
|
430
|
+
this.swept ??= this.serial(async () => {
|
|
431
|
+
if (this.stopped) return;
|
|
432
|
+
await this.deps.drop.hold();
|
|
433
|
+
const swept = await this.deps.drop.sweep(this.stillWanted);
|
|
434
|
+
if (!this.stopped) this.noteSwept(swept);
|
|
435
|
+
});
|
|
436
|
+
return this.swept;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** The question the sweep asks right before each of its effects (`StillWanted`): no, once the registry is stopped. */
|
|
440
|
+
private readonly stillWanted = (): boolean => !this.stopped;
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Called once at start. Private keys lived in memory, so nothing open can be completed: a pending request is expired by
|
|
444
|
+
* the restart. Sweeps first, if that was not done yet, so every set of an earlier run is gone: a delivered request is
|
|
445
|
+
* released by the restart. A delivered request whose set the sweep could not remove is the exception: its set is still
|
|
446
|
+
* there, so it stays delivered, in memory and in the file, and the restart owes its set (`owed`); the tick tries the set
|
|
447
|
+
* again until it is gone, and the request is then released by the restart (`settleOwed`). Says whether the requests
|
|
448
|
+
* were loaded: a registry that is stopped by the time it has read them (before the load got its turn, or while it read)
|
|
449
|
+
* keeps nothing of what it read, saves nothing and tells nobody anything, and the answer is `false`. The stored requests
|
|
450
|
+
* are then not known to this instance, and the next start is the one that loads them.
|
|
451
|
+
*/
|
|
452
|
+
async load(): Promise<boolean> {
|
|
453
|
+
await this.sweep();
|
|
454
|
+
return this.serial(async () => {
|
|
455
|
+
const state = await readStored(join(this.deps.dataDir, REQUESTS_FILE), this.deps.maxStoredBytes);
|
|
456
|
+
// Asked here, after the one wait before the load's first effect: nothing has changed yet, in memory or on disk.
|
|
457
|
+
if (this.stopped) return false;
|
|
458
|
+
this.stored = state.status;
|
|
459
|
+
this.dropped = state.dropped;
|
|
460
|
+
const now = this.deps.now();
|
|
461
|
+
const changed: RequestRecord[] = [];
|
|
462
|
+
for (const record of state.records) {
|
|
463
|
+
// A time from the future would keep a record in the history for good.
|
|
464
|
+
record.createdAt = Math.min(record.createdAt, now);
|
|
465
|
+
record.updatedAt = Math.min(record.updatedAt, now);
|
|
466
|
+
this.records.set(record.id, record);
|
|
467
|
+
if (record.state === "pending") {
|
|
468
|
+
record.state = "expired";
|
|
469
|
+
record.expiredBy = "restart";
|
|
470
|
+
} else if (record.state === "delivered") {
|
|
471
|
+
// Released only once its set is gone: one the sweep left is owed, and the request is that set's.
|
|
472
|
+
const name = setName(record.id);
|
|
473
|
+
if (this.owed.has(name)) {
|
|
474
|
+
this.owed.set(name, record.id);
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
record.state = "released";
|
|
478
|
+
record.releasedBy = "restart";
|
|
479
|
+
} else continue;
|
|
480
|
+
record.updatedAt = now;
|
|
481
|
+
changed.push(record);
|
|
482
|
+
}
|
|
483
|
+
// A file that could not be read is left as it is until the first change is saved.
|
|
484
|
+
if (state.status === "ok") await this.persist();
|
|
485
|
+
// Only what the load itself ended is told. A record that was closed when it was saved was told of then, and what
|
|
486
|
+
// is still owed for it waits in the outbox file; told again, every restart would repeat a message.
|
|
487
|
+
for (const record of changed) this.notify(record, "state");
|
|
488
|
+
return true;
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** A new pending request of `agent`'s. `call`: the terms of the tool call that asks for it (`admit`). */
|
|
493
|
+
create(agent: AgentIdentity, raw: unknown, call?: CallTerms): Promise<{ status: "pending"; requestId: string; next: string }> {
|
|
494
|
+
return this.admit(call, async (): Promise<{ status: "pending"; requestId: string; next: string }> => {
|
|
495
|
+
// A key made now would outlive the shutdown that was meant to wipe every one. Nothing is awaited from here to
|
|
496
|
+
// where the key is held, so a shutdown cannot begin in between.
|
|
497
|
+
if (this.stopped) throw new SecureInputError("service_unavailable", SHUTTING_DOWN);
|
|
498
|
+
const parsed = InputRequestSchema.safeParse(raw);
|
|
499
|
+
if (!parsed.success) {
|
|
500
|
+
// The issues quote the caller's input, invisible characters and all.
|
|
501
|
+
const issues = parsed.error.issues.slice(0, 5).map((issue) => `${issue.path.map(String).join(".") || "request"}: ${issue.message}`);
|
|
502
|
+
throw new SecureInputError("invalid_request", sanitizeText(issues.join("; "), DETAIL_MAX));
|
|
503
|
+
}
|
|
504
|
+
const existing = [...this.records.values()].find((record) => record.agentId === agent.agentId && record.state === "pending");
|
|
505
|
+
if (existing) throw new SecureInputError("request_already_pending", existing.id);
|
|
506
|
+
|
|
507
|
+
const request = cleanRequest(parsed.data);
|
|
508
|
+
const now = this.deps.now();
|
|
509
|
+
const title = typeof agent.title === "string" ? sanitizeText(agent.title, TITLE_MAX) : "";
|
|
510
|
+
// Last, so that nothing which can throw lies between making the private key and holding it.
|
|
511
|
+
const keys = generateRequestKeyPair();
|
|
512
|
+
let record: RequestRecord | null;
|
|
513
|
+
try {
|
|
514
|
+
// What is made here is what the next start will read back: the identity is held to the rules of the stored record.
|
|
515
|
+
record = parseRecord({
|
|
516
|
+
id: `r_${randomBytes(16).toString("hex")}`,
|
|
517
|
+
agentId: agent.agentId,
|
|
518
|
+
agentCwd: agent.cwd,
|
|
519
|
+
agentTitle: title === "" ? null : title,
|
|
520
|
+
request,
|
|
521
|
+
state: "pending",
|
|
522
|
+
publicKey: toBase64Url(keys.publicKey),
|
|
523
|
+
requestDigest: requestDigestOf(request),
|
|
524
|
+
retries: 0,
|
|
525
|
+
tamper: false,
|
|
526
|
+
errorCode: null,
|
|
527
|
+
fulfilledDigest: null,
|
|
528
|
+
written: null,
|
|
529
|
+
deleteBy: null,
|
|
530
|
+
createdAt: now,
|
|
531
|
+
updatedAt: now,
|
|
532
|
+
});
|
|
533
|
+
if (record === null) throw new SecureInputError("invalid_request", "the agent's identity cannot be stored");
|
|
534
|
+
// The view the dialog will be given for this request, built now and held to the dialog's own contract. A
|
|
535
|
+
// request whose view the contract refuses would be pending with a card nobody can open: it could be neither
|
|
536
|
+
// answered nor declined, and would hold the agent's one request until it is cancelled or its time is up. So
|
|
537
|
+
// it is refused here instead, before the request exists.
|
|
538
|
+
const view = RequestViewSchema.safeParse(composeView(record));
|
|
539
|
+
if (!view.success) {
|
|
540
|
+
// The path into the view, never the issue's message: the path is made of the view's own field names.
|
|
541
|
+
const where = sanitizeText((view.error.issues[0]?.path ?? []).map(String).join("."), VIEW_PATH_MAX);
|
|
542
|
+
throw new SecureInputError("invalid_request", where === "" ? NO_VIEW : `${NO_VIEW} (${where})`);
|
|
543
|
+
}
|
|
544
|
+
this.records.set(record.id, record);
|
|
545
|
+
this.keys.set(record.id, keys.secretKey);
|
|
546
|
+
} catch (error) {
|
|
547
|
+
keys.secretKey.fill(0);
|
|
548
|
+
throw error;
|
|
549
|
+
}
|
|
550
|
+
await this.commit(record, "state");
|
|
551
|
+
return { status: "pending", requestId: record.id, next: NEXT };
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** A copy: what a caller does to it changes nothing here. */
|
|
556
|
+
get(id: string): RequestRecord | undefined {
|
|
557
|
+
const record = this.records.get(id);
|
|
558
|
+
return record === undefined ? undefined : copyOf(record);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Takes a submission: opens it with the request's key, bound to the request and to what it asked for, reads it,
|
|
563
|
+
* holds it to the request, and writes it as the request's set in `drop/`. The first submission that is delivered
|
|
564
|
+
* wins; the same one sent again is answered as it was, and a different one is tampering. A submission that cannot
|
|
565
|
+
* be opened is counted and may be tried again, up to `MAX_RETRIES`; one that is opened and found wrong, or whose set
|
|
566
|
+
* cannot be written, ends the request.
|
|
567
|
+
*/
|
|
568
|
+
fulfill(id: string, envelope: SealedEnvelope): Promise<FulfillResult> {
|
|
569
|
+
if (!isWellFormed(id, envelope)) return Promise.resolve(refusal("invalid_request", false));
|
|
570
|
+
return this.serial(async (): Promise<FulfillResult> => {
|
|
571
|
+
const record = this.records.get(id);
|
|
572
|
+
if (!record) return refusal("invalid_request", false);
|
|
573
|
+
const submission = digestHex(utf8.encode(envelope.ct));
|
|
574
|
+
|
|
575
|
+
if (record.state !== "pending") {
|
|
576
|
+
if (record.fulfilledDigest === submission) return { ok: true, state: record.state };
|
|
577
|
+
// Only a second, different submission to a fulfilled request is tampering.
|
|
578
|
+
// A human answering an expired or declined request is just late.
|
|
579
|
+
if (FULFILLED_STATES.has(record.state) && !record.tamper) {
|
|
580
|
+
record.tamper = true;
|
|
581
|
+
await this.commit(record, "tamper");
|
|
582
|
+
}
|
|
583
|
+
return refusal("request_closed", false);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// The timer is not what decides: a submission to a request past its time expires it.
|
|
587
|
+
if (this.deps.now() - record.createdAt > TTL_MS) {
|
|
588
|
+
await this.closePending(id, "expired");
|
|
589
|
+
return refusal("request_closed", false);
|
|
590
|
+
}
|
|
591
|
+
// After shutdown the request stays as it is: the next start expires it, and that is what the agent is told.
|
|
592
|
+
if (this.stopped) return refusal("request_closed", false);
|
|
593
|
+
const secretKey = this.keys.get(id);
|
|
594
|
+
if (!secretKey) return this.reject(record, "request_closed");
|
|
595
|
+
let plaintext: Uint8Array;
|
|
596
|
+
try {
|
|
597
|
+
plaintext = unseal(secretKey, envelope, buildAad(record.id, record.requestDigest));
|
|
598
|
+
} catch {
|
|
599
|
+
return this.retry(record, "decrypt_failed");
|
|
600
|
+
}
|
|
601
|
+
let entries: PayloadEntry[];
|
|
602
|
+
try {
|
|
603
|
+
entries = decodeValues(plaintext);
|
|
604
|
+
} catch {
|
|
605
|
+
return this.reject(record, "invalid_request");
|
|
606
|
+
} finally {
|
|
607
|
+
plaintext.fill(0);
|
|
608
|
+
}
|
|
609
|
+
return this.deliver(record, entries, submission);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Writes a submission that was opened and read as the request's set, and marks the request delivered. Nothing is
|
|
615
|
+
* saved ahead of the write: a set that was written and not saved is read back as a pending request, which the restart
|
|
616
|
+
* expires, and its set is gone with the sweep of that start. From the write on the set has its deadline, by its own
|
|
617
|
+
* timer (`schedule`); a set whose write took effect after shutdown began gets none, as the stop takes it.
|
|
618
|
+
*/
|
|
619
|
+
private async deliver(record: RequestRecord, entries: readonly PayloadEntry[], submission: string): Promise<FulfillResult> {
|
|
620
|
+
let files: DropFile[];
|
|
621
|
+
try {
|
|
622
|
+
files = validateEntries(record.request, entries).written;
|
|
623
|
+
} catch (error) {
|
|
624
|
+
// The detail names keys and nothing else, and it is all the human will hear of why: it goes with the code.
|
|
625
|
+
return this.reject(record, "invalid_request", detailOf(error));
|
|
626
|
+
}
|
|
627
|
+
try {
|
|
628
|
+
await this.deps.drop.write(record.id, files, this.beforeWrite, this.leftBehind);
|
|
629
|
+
} catch (error) {
|
|
630
|
+
// Turned back right before its effect: nothing was written, what was staged is removed by the write's own
|
|
631
|
+
// clean-up, and the request stays as it is, in memory and in the file. The next start expires it.
|
|
632
|
+
if (error instanceof ShutdownBegan) return refusal("request_closed", false);
|
|
633
|
+
return this.reject(record, "write_failed", detailOf(error));
|
|
634
|
+
} finally {
|
|
635
|
+
for (const file of files) file.bytes.fill(0);
|
|
636
|
+
}
|
|
637
|
+
record.fulfilledDigest = submission;
|
|
638
|
+
record.written = files.map((file) => file.key);
|
|
639
|
+
record.deleteBy = this.deps.now() + (this.deps.dropTtlMs ?? DROP_TTL_MS);
|
|
640
|
+
this.dropKey(record.id);
|
|
641
|
+
record.state = "delivered";
|
|
642
|
+
// Before the save, which may stall: the deadline does not wait for the disk.
|
|
643
|
+
if (!this.stopped) this.schedule(record.id);
|
|
644
|
+
// The set is in place. Saving the record is the last thing that happens and cannot undo it.
|
|
645
|
+
await this.commit(record, "state");
|
|
646
|
+
return { ok: true, state: "delivered" };
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* The registry's last word right before a set takes effect: the store asks it when every file of the set is written
|
|
651
|
+
* under the set's temporary name, right before the rename that puts the set in place (`write`). Once shutdown has
|
|
652
|
+
* begun the answer is no, however late the write gets there: a submission that was opened before shutdown began, and
|
|
653
|
+
* whose write outlasts the bound of `shutdown`, is turned back, and what it staged is removed by the write's own
|
|
654
|
+
* clean-up as after any other failed write. So a set that had not reached its rename when shutdown began never takes
|
|
655
|
+
* effect, also not after `shutdown()` has returned, where a successor's sweep could meet it. A write that is past this
|
|
656
|
+
* when shutdown begins is one rename away from done, and is not undone: the shutdown removes the set, by its releases,
|
|
657
|
+
* which come after this write in the queue, or by its sweep.
|
|
658
|
+
*/
|
|
659
|
+
private readonly beforeWrite = (): void => {
|
|
660
|
+
if (this.stopped) throw new ShutdownBegan();
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* What a write that failed could not take away of what it staged, by its name in `drop/` (`DropStore.write`): owed from
|
|
665
|
+
* here on, so the drop reads failing and the tick tries it again by itself until it is gone (`settleOwed`). It names no
|
|
666
|
+
* request: the request was rejected, and the staging directory is nobody's set.
|
|
667
|
+
*/
|
|
668
|
+
private readonly leftBehind = (name: string): void => {
|
|
669
|
+
this.owed.set(name, null);
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
private async retry(record: RequestRecord, code: ErrorCode, detail?: string): Promise<FulfillResult> {
|
|
673
|
+
record.retries += 1;
|
|
674
|
+
if (record.retries >= MAX_RETRIES) return this.reject(record, "retry_limit");
|
|
675
|
+
await this.commit(record, "other");
|
|
676
|
+
return refusal(code, true, detail);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/** Ends the request. `detail` goes to whoever submitted, with the code; the record keeps the code alone. */
|
|
680
|
+
private async reject(record: RequestRecord, code: ErrorCode, detail?: string): Promise<FulfillResult> {
|
|
681
|
+
record.errorCode = code;
|
|
682
|
+
this.dropKey(record.id);
|
|
683
|
+
record.state = "rejected";
|
|
684
|
+
await this.commit(record, "state");
|
|
685
|
+
return refusal(code, false, detail);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** A delivered request whose set is gone, released by `by`, with nothing kept for the lifetime of its set any more, and nothing owed for it. Saves nothing. */
|
|
689
|
+
private markReleased(record: RequestRecord, by: ReleasedBy): void {
|
|
690
|
+
this.stuck.delete(record.id);
|
|
691
|
+
if (this.owes(record)) this.owed.delete(setName(record.id));
|
|
692
|
+
this.forgetLifetime(record.id);
|
|
693
|
+
record.state = "released";
|
|
694
|
+
record.releasedBy = by;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Who took a set that was not there when a removal came to it: once the stop's sweep has begun, the sweep may have, and
|
|
699
|
+
* it is the stop's doing; before that, the restart's for a set the restart owes (`owes`: it was to go at the start), and
|
|
700
|
+
* the agent's for any other, since the agent is the one who could have. Read after the removal, which can be under way
|
|
701
|
+
* when the stop begins.
|
|
702
|
+
*/
|
|
703
|
+
private takenBy(record: RequestRecord): ReleasedBy {
|
|
704
|
+
if (this.sweeping) return "stop";
|
|
705
|
+
return this.owes(record) ? "restart" : "agent";
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Takes a delivered request's set away and marks the request released: by `by`, or, when the set was already gone, by
|
|
710
|
+
* whoever took it (`takenBy`): so a removal that was under way when the stop's sweep began never calls the sweep's
|
|
711
|
+
* removal the agent's. A removal the agent asked for itself (`asked`, `release`) is the agent's release whatever took
|
|
712
|
+
* the set. The store removes a link in place of the set as a link and follows none (`DropStore.release`). A set that
|
|
713
|
+
* cannot be removed is remembered as stuck (`health`), the request stays delivered and the answer is false; what
|
|
714
|
+
* would take the set later is kept: its turn mark, so the end of its agent's next turn tries again, and its timer if
|
|
715
|
+
* it has not fired. Once its deadline has passed, the tick tries again (`expire`).
|
|
716
|
+
*/
|
|
717
|
+
private async releaseSet(record: RequestRecord, by: ReleasedBy, asked = false): Promise<boolean> {
|
|
718
|
+
const removal = await this.deps.drop.release(record.id);
|
|
719
|
+
if (removal === "stuck") {
|
|
720
|
+
this.stuck.add(record.id);
|
|
721
|
+
return false;
|
|
722
|
+
}
|
|
723
|
+
this.markReleased(record, removal !== "absent" || asked ? by : this.takenBy(record));
|
|
724
|
+
await this.commit(record, "state");
|
|
725
|
+
return true;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* The agent's own release of its delivered set (`release_secret`): released by `agent`, also when the set was gone by
|
|
730
|
+
* the time the removal came to it, the stop's sweep included. The agent asked for the files to go and they went, so a
|
|
731
|
+
* delivery message that still waits has nothing to tell it (`releasedBy` `agent` drops it), whatever took them. A
|
|
732
|
+
* request that is already released, by whatever, is answered as it is and nothing changes, so a release is made once.
|
|
733
|
+
* Another agent's request is answered as one that does not exist; a request that never had files, and one whose files
|
|
734
|
+
* cannot be removed (it stays delivered, and the plugin tries again), are refused in fixed words. A copy is returned.
|
|
735
|
+
* `call`: the terms of the tool call that asks for it (`admit`).
|
|
736
|
+
*/
|
|
737
|
+
release(agentId: string, id: string, call?: CallTerms): Promise<RequestRecord> {
|
|
738
|
+
return this.admit(call, async () => {
|
|
739
|
+
const record = this.records.get(id);
|
|
740
|
+
if (record === undefined || record.agentId !== agentId) throw new SecureInputError("invalid_request", UNKNOWN_REQUEST);
|
|
741
|
+
if (record.state === "released") return copyOf(record);
|
|
742
|
+
if (record.state !== "delivered") throw new SecureInputError("invalid_request", NO_FILES);
|
|
743
|
+
if (!(await this.releaseSet(record, "agent", true))) throw new SecureInputError("write_failed", NOT_REMOVED);
|
|
744
|
+
return copyOf(record);
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* A request as it is now, for the agent that asks about it: a delivered one whose set is gone is released first, as
|
|
750
|
+
* whoever took it (`takenBy`): the agent, who is the one who could have, or the restart for a set it owes. Who may ask is
|
|
751
|
+
* the caller's to decide, before this is called: the look can change the request. Once shutdown has begun nothing is
|
|
752
|
+
* released here, also when it began while the store was looking: the stop takes every set, and a set its sweep took is
|
|
753
|
+
* not the agent's doing. A copy, or nothing for a request this registry does not know. `call`: the terms of the tool
|
|
754
|
+
* call that asks (`admit`).
|
|
755
|
+
*/
|
|
756
|
+
check(id: string, call?: CallTerms): Promise<RequestRecord | undefined> {
|
|
757
|
+
return this.admit(call, async () => {
|
|
758
|
+
const record = this.records.get(id);
|
|
759
|
+
if (record === undefined) return undefined;
|
|
760
|
+
if (record.state !== "delivered") return copyOf(record);
|
|
761
|
+
const there = await this.deps.drop.exists(record.id);
|
|
762
|
+
// Asked after the look, which takes time: the stop may have begun before it or meanwhile.
|
|
763
|
+
if (!there && !this.stopped) await this.releaseSet(record, "agent");
|
|
764
|
+
return copyOf(record);
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* The outbox is handing a request's message to its agent, right before the send: for a delivered request, the next
|
|
770
|
+
* turn of that agent to begin is the one the message starts (`turnStarted`). Marked once; a request that is no longer
|
|
771
|
+
* delivered, and one this registry does not know, are left alone. (A mark made once shutdown has begun takes no set:
|
|
772
|
+
* `turnEnded` releases nothing from then on.)
|
|
773
|
+
*/
|
|
774
|
+
messageSent(requestId: string): void {
|
|
775
|
+
const record = this.records.get(requestId);
|
|
776
|
+
if (record?.state !== "delivered" || this.turns.has(requestId)) return;
|
|
777
|
+
this.turns.set(requestId, "sent");
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* A turn of the agent began. Each of its requests whose message was handed over before this is armed: the end of this
|
|
782
|
+
* turn takes its set. A request whose message is handed over later waits for a turn that begins after that, so a late
|
|
783
|
+
* end of the turn before the message does not take the set early.
|
|
784
|
+
*/
|
|
785
|
+
turnStarted(agentId: string): void {
|
|
786
|
+
for (const [id, mark] of this.turns) {
|
|
787
|
+
if (mark === "sent" && this.records.get(id)?.agentId === agentId) this.turns.set(id, "armed");
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/**
|
|
792
|
+
* A turn of the agent ended, with whatever outcome. The requests of that agent that are armed are taken now, as the
|
|
793
|
+
* turn ends, and their sets are released in the queue as `turn` (as whoever took it, `takenBy`, for a set that is gone
|
|
794
|
+
* by then), each that is still delivered. Taken now, and not when the queue comes to it: a request armed while this
|
|
795
|
+
* waits in the queue was armed by a turn that began after this one, and its set is that turn's. A set that is stuck
|
|
796
|
+
* keeps its mark, and the end of the next turn tries again. Once shutdown has begun no further set is released here
|
|
797
|
+
* (the stop takes them); a removal that was already under way then reads a set its sweep took as the stop's.
|
|
798
|
+
*/
|
|
799
|
+
turnEnded(agentId: string): Promise<void> {
|
|
800
|
+
const armed = [...this.turns].filter(([id, mark]) => mark === "armed" && this.records.get(id)?.agentId === agentId).map(([id]) => id);
|
|
801
|
+
return this.serial(async () => {
|
|
802
|
+
for (const id of armed) {
|
|
803
|
+
// For each request, as the tick does: the stop can begin while one of them is removed.
|
|
804
|
+
if (this.stopped) return;
|
|
805
|
+
const record = this.records.get(id);
|
|
806
|
+
if (record?.state === "delivered") await this.releaseSet(record, "turn");
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
private async closePending(id: string, state: "declined" | "cancelled" | "expired"): Promise<RequestRecord> {
|
|
812
|
+
const record = this.require(id);
|
|
813
|
+
if (record.state !== "pending") throw new SecureInputError("request_closed");
|
|
814
|
+
this.dropKey(id);
|
|
815
|
+
record.state = state;
|
|
816
|
+
// By its time: `load` marks the ones a restart expired.
|
|
817
|
+
if (state === "expired") record.expiredBy = "time";
|
|
818
|
+
await this.commit(record, "state");
|
|
819
|
+
return copyOf(record);
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
decline(id: string): Promise<RequestRecord> {
|
|
823
|
+
return this.serial(() => this.closePending(id, "declined"));
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/** Withdraws a pending request of the agent's own. `call`: the terms of the tool call that asks for it (`admit`). */
|
|
827
|
+
cancel(agentId: string, id: string, call?: CallTerms): Promise<RequestRecord> {
|
|
828
|
+
return this.admit(call, async () => {
|
|
829
|
+
if (this.require(id).agentId !== agentId) throw new SecureInputError("request_closed");
|
|
830
|
+
return this.closePending(id, "cancelled");
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* The minute tick. What `drop/` owes is tried again first, entry by entry (`settleOwed`). A pending request past its
|
|
836
|
+
* time expires. A delivered set goes when the wall clock has reached its deadline (`time`), whether or not its timer
|
|
837
|
+
* fired (a machine that slept fires none on time), and so a set past its deadline that could not be removed before is
|
|
838
|
+
* tried again at every tick. Before its deadline, a set the agent took away itself is noticed (`agent`). A set the
|
|
839
|
+
* restart owes is the owed one's to try, and not tried a second time here. Once shutdown has begun no further set is
|
|
840
|
+
* released here, also when it began while the store was looking: the stop takes them, and a set its sweep took is not
|
|
841
|
+
* the agent's doing. A removal that was under way when the stop began calls a set its sweep took the stop's (`takenBy`).
|
|
842
|
+
*/
|
|
843
|
+
expire(): Promise<void> {
|
|
844
|
+
return this.serial(async () => {
|
|
845
|
+
const now = this.deps.now();
|
|
846
|
+
await this.settleOwed();
|
|
847
|
+
for (const record of [...this.records.values()]) {
|
|
848
|
+
if (record.state === "pending" && now - record.createdAt > TTL_MS) await this.closePending(record.id, "expired");
|
|
849
|
+
else if (record.state === "delivered" && !this.stopped && !this.owes(record)) {
|
|
850
|
+
if (record.deleteBy !== null && now >= record.deleteBy) await this.releaseSet(record, "time");
|
|
851
|
+
// Asked again after the look, which takes time: the stop may have begun meanwhile.
|
|
852
|
+
else if (!(await this.deps.drop.exists(record.id)) && !this.stopped) await this.releaseSet(record, "agent");
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
// A save that failed is tried again here, not only at the next change.
|
|
856
|
+
if (this.persistence === "failing") await this.persist();
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* What `drop/` owes, tried again: each entry by itself, by its name (`DropStore.remove`, under the root's guard), and
|
|
862
|
+
* never by a sweep, which would take the live sets of other requests too. An entry that is gone, by this removal or
|
|
863
|
+
* before it, is owed no more. The set of a request the restart owes is released with it, as `restart` (`releaseSet`;
|
|
864
|
+
* the stop's, once its sweep has begun): its request was restored delivered and is released only now that its set is
|
|
865
|
+
* gone. Once shutdown has begun nothing more is tried here: the stop sweeps `drop/`.
|
|
866
|
+
*/
|
|
867
|
+
private async settleOwed(): Promise<void> {
|
|
868
|
+
for (const [name, requestId] of [...this.owed]) {
|
|
869
|
+
if (this.stopped) return;
|
|
870
|
+
const record = requestId === null ? undefined : this.records.get(requestId);
|
|
871
|
+
if (record?.state === "delivered") await this.releaseSet(record, "restart");
|
|
872
|
+
else if ((await this.deps.drop.remove(name)) !== "stuck") this.owed.delete(name);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* The human's word to delete every set at once (the settings screen). It is done in the queue: a submission whose set is
|
|
878
|
+
* being written is written first and then deleted, and one that comes after is delivered as usual. Every delivered
|
|
879
|
+
* request's set is released as `human` (`releaseSet`: its timer and turn mark go, the request is saved and told of), and
|
|
880
|
+
* then `drop/` is swept of whatever else lies in it: the leftovers of a write that was killed, what was put there.
|
|
881
|
+
*
|
|
882
|
+
* Says how many requests it released and how many entries of `drop/` are still there after the sweep. A set that would
|
|
883
|
+
* not go at first and that the sweep then took is the human's doing as well, and counts as released: the sweep names
|
|
884
|
+
* what it took, and asks before each removal whether the stop has begun, so it took nothing after that. A set that
|
|
885
|
+
* would not go at first and that is gone though the sweep did not take it went some other way, and is released as
|
|
886
|
+
* whoever took it (`takenBy`: the agent, the restart for a set it owes, the stop once its sweep has begun), never as
|
|
887
|
+
* the human. What the sweep left is owed (`noteSwept`) and tried again at the tick. While any entry is still there
|
|
888
|
+
* `health` says the drop is failing, and so it does after a sweep that could not run: that fails the call with the
|
|
889
|
+
* sweep's own refusal, and what was released before stays released.
|
|
890
|
+
*
|
|
891
|
+
* A registry that is stopping refuses it, and so does one that begins to stop on the way: the stop takes the sets, and
|
|
892
|
+
* no sweep is begun here after it, since a sweep that is told to stop counts the entry it stopped at as stuck. A stop
|
|
893
|
+
* that begins while the sweep runs is asked about after it, and again after each look at a set: from then on the stop
|
|
894
|
+
* owns the sets and the account of `drop/`, what this sweep left is not noted, and the call is refused once the sets it
|
|
895
|
+
* found gone are released as whoever took them.
|
|
896
|
+
*/
|
|
897
|
+
clearDrop(): Promise<{ released: number; stuck: number }> {
|
|
898
|
+
return this.serial(async () => {
|
|
899
|
+
let released = 0;
|
|
900
|
+
const unremoved: RequestRecord[] = [];
|
|
901
|
+
for (const record of [...this.records.values()]) {
|
|
902
|
+
// Asked before each set, as the tick does: the stop can begin while one of them is removed.
|
|
903
|
+
if (this.stopped) break;
|
|
904
|
+
if (record.state !== "delivered") continue;
|
|
905
|
+
if (await this.releaseSet(record, "human")) released += 1;
|
|
906
|
+
else unremoved.push(record);
|
|
907
|
+
}
|
|
908
|
+
if (this.stopped) throw new SecureInputError("service_unavailable", SHUTTING_DOWN);
|
|
909
|
+
const swept = await this.deps.drop.sweep(this.stillWanted).catch((error: unknown) => {
|
|
910
|
+
this.sweepFailed = true;
|
|
911
|
+
throw error;
|
|
912
|
+
});
|
|
913
|
+
// Asked again after the sweep, which takes time: once the stop has begun, `drop/` is the stop's to account for.
|
|
914
|
+
if (!this.stopped) this.noteSwept(swept);
|
|
915
|
+
const took = new Set(swept.took);
|
|
916
|
+
for (const record of unremoved) {
|
|
917
|
+
if (took.has(setName(record.id))) {
|
|
918
|
+
// The sweep took it, at the human's word, before the stop began: told so (`asked`), since found gone it would
|
|
919
|
+
// be called someone else's.
|
|
920
|
+
if (await this.releaseSet(record, "human", true)) released += 1;
|
|
921
|
+
} else if (!(await this.deps.drop.exists(record.id)) && (await this.releaseSet(record, "human"))) {
|
|
922
|
+
// Gone, though not by the sweep: as whoever took it (`takenBy`), read after the look, when the stop may have begun.
|
|
923
|
+
released += 1;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
if (this.stopped) throw new SecureInputError("service_unavailable", SHUTTING_DOWN);
|
|
927
|
+
return { released, stuck: swept.stuck };
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* Wipes every key still held, releases every delivered set and sweeps `drop/`. Safe to call again: the work is done once.
|
|
933
|
+
*
|
|
934
|
+
* What cannot wait is done at once, whatever the work queue is doing: nothing new starts, every key is wiped, and every
|
|
935
|
+
* set's timer and turn mark go, so that no set is released by its deadline or by the end of a turn from here on. A
|
|
936
|
+
* submission that has not been opened yet finds no key, and its request stays as it is. A task stuck in the queue would
|
|
937
|
+
* otherwise keep the keys in memory for as long as it is stuck.
|
|
938
|
+
*
|
|
939
|
+
* Then, in the queue and within `SHUTDOWN_BOUND_MS`, every delivered set is released as `stop` (as whoever took it,
|
|
940
|
+
* `takenBy`, when it was gone before: the agent, or the restart for a set it owes), and one save covers them all, and a
|
|
941
|
+
* save that had failed before. Then, whether or not the queue came to that within its bound, `drop/` is swept within
|
|
942
|
+
* `DROP_SWEEP_BOUND_MS`; a sweep that is still under way at that bound removes nothing more (`stillWanted`), since
|
|
943
|
+
* `drop/` may be a successor's by then. The two bounds end well inside the 2 seconds Paseo 0.14 gives the plugin's
|
|
944
|
+
* process to stop (`PASEO_STOP_WINDOW_MS` in `server/paseo/plugin.ts`). So even when a task in the queue never ends, the
|
|
945
|
+
* stop sweeps `drop/` before the process is ended, and the only set that outlives the plugin is one the sweep cannot
|
|
946
|
+
* remove within its half second (a disk that stalls, a set it may not change). The sweep of the next start takes it,
|
|
947
|
+
* and that start's `load` settles its request: released by the restart once the set is gone, owed until then. It is
|
|
948
|
+
* not reported here, since the process ends right after the stop and nothing reads `health` from then on: a delivered
|
|
949
|
+
* set that the sweep left is neither owed nor stuck (`noteSwept`), unless a removal before the sweep, the stop's own
|
|
950
|
+
* or an earlier one, found it stuck.
|
|
951
|
+
*
|
|
952
|
+
* When the queue has not come to the releases by the time the sweep begins, the sweep takes the sets, and from then on
|
|
953
|
+
* a set that is not there may be the sweep's doing, not the agent's (`sweeping`). So the releases of the stop end there.
|
|
954
|
+
* The other ways a set goes (`check`, `expire`, `turnEnded`, the timers) begin no release once shutdown has begun, and
|
|
955
|
+
* the looks among them ask again after the store has looked; a removal of theirs that was already under way when the
|
|
956
|
+
* stop began reads a set that is gone by then through `takenBy`, as the stop's once the sweep has begun. So no request
|
|
957
|
+
* reads `agent` because the stop removed its set, in memory or in the file. Only the agent's own word (`release`) reads
|
|
958
|
+
* `agent` whatever took the set: the agent asked for the files to go. The requests the releases did not reach stay
|
|
959
|
+
* delivered, in memory and in the file, and the next start reads them back as released by the restart.
|
|
960
|
+
*
|
|
961
|
+
* A task that is still running when the bounds pass is not cut off, which would only let it write later with nobody
|
|
962
|
+
* watching. It finds `stopped` before each effect instead: no key is made and no set takes effect (`create`,
|
|
963
|
+
* `beforeWrite`). From the end of the sweep's bound this instance starts no save and tells nobody of a change. A save
|
|
964
|
+
* that was already under way then is not cut off either: it lands, and its change is announced by nobody, neither by
|
|
965
|
+
* this instance nor by the next start, which reads the record back as it was saved (SECURITY.md says so). A set whose
|
|
966
|
+
* rename was already past its last check when shutdown began is not undone: the releases of the stop take it as any
|
|
967
|
+
* other, or the sweep does, and if the save of its request is skipped, the next start reads the request back as open
|
|
968
|
+
* and expires it.
|
|
969
|
+
*/
|
|
970
|
+
shutdown(): Promise<void> {
|
|
971
|
+
this.ending ??= this.end();
|
|
972
|
+
return this.ending;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
/**
|
|
976
|
+
* Marks the registry stopped, and does nothing else: the first thing `shutdown` does, for a caller that has
|
|
977
|
+
* something to do between the two. The plugin calls it the moment it stops waiting for its start-up, so that a
|
|
978
|
+
* sweep or a load of that start-up which comes back later finds the mark, however much of the dispose is still to
|
|
979
|
+
* come (`sweep`, `load`). Everything that asks "has shutdown begun" answers yes from here on.
|
|
980
|
+
*/
|
|
981
|
+
stop(): void {
|
|
982
|
+
this.stopped = true;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
private async end(): Promise<void> {
|
|
986
|
+
this.stopped = true;
|
|
987
|
+
for (const id of [...this.keys.keys()]) this.dropKey(id);
|
|
988
|
+
for (const id of [...this.timers.keys()]) this.forgetLifetime(id);
|
|
989
|
+
this.turns.clear();
|
|
990
|
+
|
|
991
|
+
// Behind the work that is in the queue now, and so behind the saves it makes.
|
|
992
|
+
const queued = this.serial(async () => {
|
|
993
|
+
const released: RequestRecord[] = [];
|
|
994
|
+
for (const record of [...this.records.values()]) {
|
|
995
|
+
// The sweep has begun: what is left is the sweep's to take.
|
|
996
|
+
if (this.sweeping) break;
|
|
997
|
+
if (record.state !== "delivered") continue;
|
|
998
|
+
const removal = await this.deps.drop.release(record.id);
|
|
999
|
+
if (removal === "stuck") {
|
|
1000
|
+
this.stuck.add(record.id);
|
|
1001
|
+
continue;
|
|
1002
|
+
}
|
|
1003
|
+
// Not there: the agent took it before the stop came to it (the restart, for a set it owes), unless the sweep began
|
|
1004
|
+
// meanwhile, which takes sets too.
|
|
1005
|
+
this.markReleased(record, removal === "absent" ? this.takenBy(record) : "stop");
|
|
1006
|
+
record.updatedAt = this.deps.now();
|
|
1007
|
+
released.push(record);
|
|
1008
|
+
}
|
|
1009
|
+
// One save for every set the stop released, and for a save that had failed before; then whoever is told hears of
|
|
1010
|
+
// each, as after any change (`commit`). An instance that is done saves nothing and tells nobody.
|
|
1011
|
+
if (this.finished) return;
|
|
1012
|
+
if (released.length > 0 || this.persistence === "failing") await this.persist();
|
|
1013
|
+
for (const record of released) this.notify(record, "state");
|
|
1014
|
+
this.tellForgotten();
|
|
1015
|
+
});
|
|
1016
|
+
// Nothing in it rejects; if something ever did, shutdown would still go on.
|
|
1017
|
+
await raceDeadline(this.deps.shutdownBoundMs ?? SHUTDOWN_BOUND_MS, undefined, () => queued.catch(() => undefined));
|
|
1018
|
+
|
|
1019
|
+
this.sweeping = true;
|
|
1020
|
+
let sweepOver = false;
|
|
1021
|
+
const swept = this.deps.drop.sweep(() => !sweepOver).then(
|
|
1022
|
+
(result) => this.noteSwept(result),
|
|
1023
|
+
() => {
|
|
1024
|
+
this.sweepFailed = true;
|
|
1025
|
+
},
|
|
1026
|
+
);
|
|
1027
|
+
await raceDeadline(DROP_SWEEP_BOUND_MS, undefined, () => swept);
|
|
1028
|
+
sweepOver = true;
|
|
1029
|
+
this.finished = true;
|
|
1030
|
+
}
|
|
1031
|
+
}
|