@mostra/runtime 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +39 -2
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-CzBkUyHu.js +15920 -0
- package/dist/instance-CzBkUyHu.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -3
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +760 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/identity.ts +101 -0
- package/src/gateway/index.ts +1299 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +979 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1013 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +187 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +596 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +518 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server functions as pages call them (D54, ARCHITECTURE §7): plain calls, which stream their
|
|
3
|
+
* result and leave no record, and durable action runs (AC-02 to AC-04), which the interaction core
|
|
4
|
+
* records before a worker starts them and which outlive the tab that started them. Runs stream
|
|
5
|
+
* progress, log lines and partial results to every view that follows them, as ephemeral messages,
|
|
6
|
+
* and keep them with the run, so a reloaded page re-attaches and reads what it missed.
|
|
7
|
+
*
|
|
8
|
+
* This module also answers what server code asks of Mostra (the worker bridge): secrets by name,
|
|
9
|
+
* signals and host events, and the artifact's shared state.
|
|
10
|
+
*/
|
|
11
|
+
import {
|
|
12
|
+
ACTION_ARGS_MAX,
|
|
13
|
+
ACTION_RESULT_MAX,
|
|
14
|
+
type Batches,
|
|
15
|
+
type CallLine,
|
|
16
|
+
checkArgumentFrames,
|
|
17
|
+
createDiagnostic,
|
|
18
|
+
type Frame,
|
|
19
|
+
RpcError,
|
|
20
|
+
type RunDelta,
|
|
21
|
+
type RunProgress,
|
|
22
|
+
type RunState,
|
|
23
|
+
ServerEmitParams,
|
|
24
|
+
ServerSecretParams,
|
|
25
|
+
ServerSignalParams,
|
|
26
|
+
ServerStateGetParams,
|
|
27
|
+
ServerStatePatchParams,
|
|
28
|
+
type WireError,
|
|
29
|
+
WORKER_METHODS,
|
|
30
|
+
} from "@mostra/protocol";
|
|
31
|
+
import type { EphemeralBus, Interaction, PageIdentity, RunRow } from "../interaction/index.ts";
|
|
32
|
+
import { type Log, type Redactor, redactFrames, redactValue } from "../log.ts";
|
|
33
|
+
import type { Secrets } from "../secrets/index.ts";
|
|
34
|
+
import type {
|
|
35
|
+
WorkerBridge,
|
|
36
|
+
WorkerCrash,
|
|
37
|
+
WorkerHandle,
|
|
38
|
+
WorkerPool,
|
|
39
|
+
WorkerVersion,
|
|
40
|
+
} from "../workers/index.ts";
|
|
41
|
+
|
|
42
|
+
/** How often a running run's output is written and sent to its views. */
|
|
43
|
+
const FLUSH_MS = 200;
|
|
44
|
+
|
|
45
|
+
/** Options for {@link createActions}. */
|
|
46
|
+
export interface ActionsOptions {
|
|
47
|
+
readonly interaction: Interaction;
|
|
48
|
+
readonly pool: WorkerPool;
|
|
49
|
+
readonly bus: EphemeralBus;
|
|
50
|
+
readonly secrets: Secrets;
|
|
51
|
+
readonly redactor: Redactor;
|
|
52
|
+
readonly log: Log;
|
|
53
|
+
/** What the pool needs to start a version's worker, or `undefined` for a version without server code. */
|
|
54
|
+
readonly version: (versionId: string) => WorkerVersion | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* A verify run's dry-run policy (D55, D138). With it, a function that opted into `dryRun()` runs
|
|
57
|
+
* with `dryRun: true`, and any other is refused with `MOSTRA_VERIFY_ACTION_SKIPPED` before a run
|
|
58
|
+
* is recorded or a worker starts, for plain calls and runs alike. Absent for viewers' pages.
|
|
59
|
+
*/
|
|
60
|
+
readonly dryRun?: {
|
|
61
|
+
readonly optedIn: (versionId: string, fnId: string) => boolean;
|
|
62
|
+
/** Told about each refused function, for the verdict's `skippedActions`. */
|
|
63
|
+
readonly skipped: (fnId: string) => void;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Plain calls and action runs, and the bridge workers ask through. */
|
|
68
|
+
export interface Actions {
|
|
69
|
+
/**
|
|
70
|
+
* A plain call (D54): resolves once its worker has it (refusals throw here), with the result's
|
|
71
|
+
* batches as they come and then how it ended.
|
|
72
|
+
*/
|
|
73
|
+
call(
|
|
74
|
+
page: PageIdentity,
|
|
75
|
+
input: { readonly viewId: string; readonly fnId: string; readonly args: Batches },
|
|
76
|
+
signal: AbortSignal,
|
|
77
|
+
): Promise<AsyncIterable<CallLine>>;
|
|
78
|
+
/** Starts or joins a run (AC-02, AC-03); the page follows it from here. */
|
|
79
|
+
startRun(
|
|
80
|
+
page: PageIdentity,
|
|
81
|
+
input: {
|
|
82
|
+
readonly viewId: string;
|
|
83
|
+
readonly fnId: string;
|
|
84
|
+
readonly args: Batches;
|
|
85
|
+
readonly key?: string;
|
|
86
|
+
readonly clientKey: string;
|
|
87
|
+
},
|
|
88
|
+
): RunState;
|
|
89
|
+
/** Reads runs and follows them for a minute (re-attach, AC-02). */
|
|
90
|
+
attach(
|
|
91
|
+
page: PageIdentity,
|
|
92
|
+
input: {
|
|
93
|
+
readonly viewId: string;
|
|
94
|
+
readonly runIds?: readonly string[];
|
|
95
|
+
readonly keyed?: readonly { readonly fnId: string; readonly key: string }[];
|
|
96
|
+
},
|
|
97
|
+
): RunState[];
|
|
98
|
+
/** Cancels a run (AC-04): its `abortSignal` fires; a queued run ends at once. */
|
|
99
|
+
cancel(page: PageIdentity, viewId: string, runId: string): RunState;
|
|
100
|
+
/** What workers ask of the supervisor. */
|
|
101
|
+
readonly bridge: Omit<WorkerBridge, "liveValue">;
|
|
102
|
+
/** A worker crashed: its runs end `interrupted` (D24), the crash is an event. */
|
|
103
|
+
crashed(crash: WorkerCrash): void;
|
|
104
|
+
/** Ends the runs a stopped instance left open as `interrupted` (FAILURE_MODEL §2). */
|
|
105
|
+
recover(): void;
|
|
106
|
+
/** How many runs and calls are in flight: the daemon stays while any are (D62). */
|
|
107
|
+
activity(): number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A run this instance is executing. */
|
|
111
|
+
interface Executing {
|
|
112
|
+
handle?: WorkerHandle;
|
|
113
|
+
callId?: string;
|
|
114
|
+
cancelled: boolean;
|
|
115
|
+
/** Ends the run now: set while it waits for its worker, so a cancel needn't wait for one. */
|
|
116
|
+
endNow?: () => void;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function failed(code: string, message: string): RpcError {
|
|
120
|
+
return new RpcError(createDiagnostic(code as never, { message }));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Creates the actions module. */
|
|
124
|
+
export function createActions(options: ActionsOptions): Actions {
|
|
125
|
+
const { interaction, pool, bus, log } = options;
|
|
126
|
+
/** Runs executing in this instance: their worker call, for cancel and crashes. */
|
|
127
|
+
const executing = new Map<string, Executing>();
|
|
128
|
+
/** Which run each worker call is, for crashes. */
|
|
129
|
+
const callRuns = new Map<string, string>();
|
|
130
|
+
/** Why a run's worker stopped under it, until its `execute` ends it. */
|
|
131
|
+
const crashCauses = new Map<string, string>();
|
|
132
|
+
let calls = 0;
|
|
133
|
+
|
|
134
|
+
const redact = (text: string): string => options.redactor.redact(text);
|
|
135
|
+
const redactError = (error: WireError): WireError => ({
|
|
136
|
+
message: redact(error.message),
|
|
137
|
+
...(error.code ? { code: error.code } : {}),
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const versionOf = (versionId: string): WorkerVersion => {
|
|
141
|
+
const version = options.version(versionId);
|
|
142
|
+
if (!version) {
|
|
143
|
+
throw failed(
|
|
144
|
+
"MOSTRA_ACTION_UNKNOWN_FUNCTION",
|
|
145
|
+
"This version has no server code, so it has no server functions to call.",
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return version;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The view's version, which must have the function: a page can't record runs, start workers or
|
|
153
|
+
* spend a key for a function its version doesn't have (INTERFACES §4; Opus review 6).
|
|
154
|
+
*/
|
|
155
|
+
const functionOf = (versionId: string, fnId: string): WorkerVersion => {
|
|
156
|
+
const version = versionOf(versionId);
|
|
157
|
+
if (!version.functions.has(fnId)) {
|
|
158
|
+
throw failed(
|
|
159
|
+
"MOSTRA_ACTION_UNKNOWN_FUNCTION",
|
|
160
|
+
`This version has no server function ${fnId.slice(0, 200)}.`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
const policy = options.dryRun;
|
|
164
|
+
if (policy && !policy.optedIn(versionId, fnId)) {
|
|
165
|
+
policy.skipped(fnId);
|
|
166
|
+
throw failed(
|
|
167
|
+
"MOSTRA_VERIFY_ACTION_SKIPPED",
|
|
168
|
+
`Skipped in verify: the server function ${fnId.slice(0, 200)} doesn't check dryRun(), so verify never runs it.`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
return version;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const checkArgs = (args: Batches): void => {
|
|
175
|
+
if (JSON.stringify(args).length > ACTION_ARGS_MAX) {
|
|
176
|
+
throw failed(
|
|
177
|
+
"MOSTRA_ACTION_TOO_LARGE",
|
|
178
|
+
"The serialized arguments are larger than 16 MiB; pass less data, such as identifiers.",
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
// What a worker would refuse is refused before one starts or a run is recorded: a page can't
|
|
182
|
+
// make server code hold memory for the life of its process (Opus review 1).
|
|
183
|
+
try {
|
|
184
|
+
checkArgumentFrames(args);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
throw failed("MOSTRA_ACTION_UNSERIALIZABLE", (error as Error).message);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const publish = (artifactId: string, run: RunDelta): void =>
|
|
191
|
+
bus.publishRun(artifactId, { kind: "run", run });
|
|
192
|
+
|
|
193
|
+
/** Runs a recorded run in its version's worker, writing its output as it comes. */
|
|
194
|
+
const execute = async (row: RunRow): Promise<void> => {
|
|
195
|
+
const entry: Executing = { cancelled: false };
|
|
196
|
+
executing.set(row.id, entry);
|
|
197
|
+
let pendingLines: string[] = [];
|
|
198
|
+
let pendingBatches: Frame[][] = [];
|
|
199
|
+
let pendingProgress: RunProgress | undefined;
|
|
200
|
+
let tooLarge = false;
|
|
201
|
+
let size = 0;
|
|
202
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
203
|
+
|
|
204
|
+
const flush = (): void => {
|
|
205
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
206
|
+
timer = undefined;
|
|
207
|
+
if (pendingLines.length === 0 && pendingBatches.length === 0 && pendingProgress === undefined)
|
|
208
|
+
return;
|
|
209
|
+
const output = {
|
|
210
|
+
lines: pendingLines,
|
|
211
|
+
batches: pendingBatches,
|
|
212
|
+
...(pendingProgress ? { progress: pendingProgress } : {}),
|
|
213
|
+
};
|
|
214
|
+
pendingLines = [];
|
|
215
|
+
pendingBatches = [];
|
|
216
|
+
const progress = pendingProgress;
|
|
217
|
+
pendingProgress = undefined;
|
|
218
|
+
const written = interaction.runOutput(row.id, output);
|
|
219
|
+
if (!written) return;
|
|
220
|
+
publish(row.artifact_id, {
|
|
221
|
+
runId: row.id,
|
|
222
|
+
status: "running",
|
|
223
|
+
...(progress ? { progress } : {}),
|
|
224
|
+
logFrom: written.logFrom,
|
|
225
|
+
log: written.log,
|
|
226
|
+
batchFrom: written.batchFrom,
|
|
227
|
+
batches: written.batches,
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
const soon = (): void => {
|
|
231
|
+
timer ??= setTimeout(flush, FLUSH_MS);
|
|
232
|
+
};
|
|
233
|
+
const end = (
|
|
234
|
+
status: "succeeded" | "failed" | "cancelled",
|
|
235
|
+
reason?: string,
|
|
236
|
+
error?: WireError,
|
|
237
|
+
): void => {
|
|
238
|
+
flush();
|
|
239
|
+
const state = interaction.endRun(row.id, {
|
|
240
|
+
status,
|
|
241
|
+
...(reason ? { reason } : {}),
|
|
242
|
+
...(error ? { error: redactError(error) } : {}),
|
|
243
|
+
});
|
|
244
|
+
if (state) {
|
|
245
|
+
publish(row.artifact_id, {
|
|
246
|
+
runId: row.id,
|
|
247
|
+
status: state.status,
|
|
248
|
+
...(state.reason ? { reason: state.reason } : {}),
|
|
249
|
+
logFrom: state.log.length,
|
|
250
|
+
log: [],
|
|
251
|
+
batchFrom: state.batches.length,
|
|
252
|
+
batches: [],
|
|
253
|
+
...(state.error ? { error: state.error } : {}),
|
|
254
|
+
...(state.finishedAt ? { finishedAt: state.finishedAt } : {}),
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
let ended = false;
|
|
260
|
+
try {
|
|
261
|
+
let handle: WorkerHandle;
|
|
262
|
+
// A cancel while the worker starts (or backs off after a crash) ends the run at once.
|
|
263
|
+
entry.endNow = () => {
|
|
264
|
+
ended = true;
|
|
265
|
+
end("cancelled", "cancelled");
|
|
266
|
+
};
|
|
267
|
+
try {
|
|
268
|
+
handle = await pool.acquire(versionOf(row.version_id));
|
|
269
|
+
} catch (error) {
|
|
270
|
+
delete entry.endNow;
|
|
271
|
+
if (ended) return;
|
|
272
|
+
const diagnostic = error instanceof RpcError ? error.diagnostic : undefined;
|
|
273
|
+
end("failed", diagnostic?.code === "MOSTRA_RUNTIME_WORKER_PARKED" ? "parked" : "crashed", {
|
|
274
|
+
code: diagnostic?.code ?? "MOSTRA_RUNTIME_WORKER_CRASHED",
|
|
275
|
+
message: diagnostic?.message ?? String(error),
|
|
276
|
+
});
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
delete entry.endNow;
|
|
280
|
+
if (ended) return;
|
|
281
|
+
if (entry.cancelled) {
|
|
282
|
+
end("cancelled", "cancelled");
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
interaction.runRunning(row.id);
|
|
286
|
+
publish(row.artifact_id, {
|
|
287
|
+
runId: row.id,
|
|
288
|
+
status: "running",
|
|
289
|
+
logFrom: 0,
|
|
290
|
+
log: [],
|
|
291
|
+
batchFrom: 0,
|
|
292
|
+
batches: [],
|
|
293
|
+
});
|
|
294
|
+
const viewer = row.viewer_id ? interaction.viewer(row.viewer_id) : undefined;
|
|
295
|
+
const { callId, outcome } = handle.call(
|
|
296
|
+
{
|
|
297
|
+
fnId: row.action,
|
|
298
|
+
args: JSON.parse(row.args_json) as Batches,
|
|
299
|
+
context: {
|
|
300
|
+
kind: "run",
|
|
301
|
+
runId: row.id,
|
|
302
|
+
...(row.view_id ? { viewId: row.view_id } : {}),
|
|
303
|
+
...(viewer
|
|
304
|
+
? { viewer: { id: viewer.id, ...(viewer.name ? { name: viewer.name } : {}) } }
|
|
305
|
+
: {}),
|
|
306
|
+
dryRun: options.dryRun !== undefined,
|
|
307
|
+
},
|
|
308
|
+
...(row.viewer_id ? { viewerId: row.viewer_id } : {}),
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
onBatch(batch) {
|
|
312
|
+
if (tooLarge) return;
|
|
313
|
+
size += JSON.stringify(batch).length;
|
|
314
|
+
if (size > ACTION_RESULT_MAX) {
|
|
315
|
+
tooLarge = true;
|
|
316
|
+
handle.cancel(callId, "The result grew past 16 MiB.");
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
pendingBatches.push(redactFrames([...batch], options.redactor));
|
|
320
|
+
soon();
|
|
321
|
+
},
|
|
322
|
+
onProgress(progress) {
|
|
323
|
+
pendingProgress = redactValue(progress, options.redactor);
|
|
324
|
+
soon();
|
|
325
|
+
},
|
|
326
|
+
onLog(line) {
|
|
327
|
+
pendingLines.push(redact(line));
|
|
328
|
+
soon();
|
|
329
|
+
},
|
|
330
|
+
},
|
|
331
|
+
);
|
|
332
|
+
entry.handle = handle;
|
|
333
|
+
entry.callId = callId;
|
|
334
|
+
callRuns.set(callId, row.id);
|
|
335
|
+
if (entry.cancelled) handle.cancel(callId, "The run was cancelled.");
|
|
336
|
+
let result: Awaited<typeof outcome>;
|
|
337
|
+
try {
|
|
338
|
+
result = await outcome;
|
|
339
|
+
} catch (error) {
|
|
340
|
+
// The worker ended during the run (D24: interrupted, never retried by Mostra).
|
|
341
|
+
const cause = crashCauses.get(row.id);
|
|
342
|
+
crashCauses.delete(row.id);
|
|
343
|
+
const message = cause
|
|
344
|
+
? `The run was interrupted: the artifact's server code stopped (${cause}).`
|
|
345
|
+
: error instanceof RpcError
|
|
346
|
+
? error.diagnostic.message
|
|
347
|
+
: String(error);
|
|
348
|
+
end("failed", "interrupted", { code: "MOSTRA_ACTION_INTERRUPTED", message });
|
|
349
|
+
return;
|
|
350
|
+
} finally {
|
|
351
|
+
callRuns.delete(callId);
|
|
352
|
+
}
|
|
353
|
+
if (tooLarge) {
|
|
354
|
+
end("failed", "too-large", {
|
|
355
|
+
code: "MOSTRA_ACTION_TOO_LARGE",
|
|
356
|
+
message: "The run's result grew past 16 MiB; the run was stopped.",
|
|
357
|
+
});
|
|
358
|
+
} else if (result.status === "succeeded") {
|
|
359
|
+
end("succeeded");
|
|
360
|
+
} else if (result.status === "cancelled") {
|
|
361
|
+
end("cancelled", "cancelled");
|
|
362
|
+
} else {
|
|
363
|
+
// The stack goes to the run's log, never to the page (INTERFACES §2.4).
|
|
364
|
+
if (result.error.stack) pendingLines.push(...redact(result.error.stack).split("\n"));
|
|
365
|
+
end(
|
|
366
|
+
"failed",
|
|
367
|
+
result.error.code === "MOSTRA_ACTION_UNSERIALIZABLE" ? "unserializable" : "threw",
|
|
368
|
+
{ code: result.error.code ?? "MOSTRA_ACTION_THREW", message: result.error.message },
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
} catch (error) {
|
|
372
|
+
log.error("action.run_failed", { runId: row.id, error });
|
|
373
|
+
end("failed", "interrupted", {
|
|
374
|
+
code: "MOSTRA_ACTION_INTERRUPTED",
|
|
375
|
+
message: "Mostra couldn't finish running this action.",
|
|
376
|
+
});
|
|
377
|
+
} finally {
|
|
378
|
+
executing.delete(row.id);
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/** A plain call's lines, from a worker that has it. */
|
|
383
|
+
async function* stream(
|
|
384
|
+
page: PageIdentity,
|
|
385
|
+
input: { readonly viewId: string; readonly fnId: string; readonly args: Batches },
|
|
386
|
+
signal: AbortSignal,
|
|
387
|
+
handle: WorkerHandle,
|
|
388
|
+
): AsyncGenerator<CallLine> {
|
|
389
|
+
const viewer = interaction.viewer(page.viewerId);
|
|
390
|
+
const queue: CallLine[] = [];
|
|
391
|
+
let wake: (() => void) | undefined;
|
|
392
|
+
let size = 0;
|
|
393
|
+
let tooLarge = false;
|
|
394
|
+
const { callId, outcome } = handle.call(
|
|
395
|
+
{
|
|
396
|
+
fnId: input.fnId,
|
|
397
|
+
args: input.args,
|
|
398
|
+
context: {
|
|
399
|
+
kind: "call",
|
|
400
|
+
viewId: input.viewId,
|
|
401
|
+
viewer: { id: viewer.id, ...(viewer.name ? { name: viewer.name } : {}) },
|
|
402
|
+
dryRun: options.dryRun !== undefined,
|
|
403
|
+
},
|
|
404
|
+
viewerId: page.viewerId,
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
onBatch(batch) {
|
|
408
|
+
if (tooLarge) return;
|
|
409
|
+
// The same bound as a run's result, so a call never buffers without limit (D131).
|
|
410
|
+
size += JSON.stringify(batch).length;
|
|
411
|
+
if (size > ACTION_RESULT_MAX) {
|
|
412
|
+
tooLarge = true;
|
|
413
|
+
handle.cancel(callId, "The result grew past 16 MiB.");
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
queue.push({ batch: redactFrames([...batch], options.redactor) });
|
|
417
|
+
wake?.();
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
);
|
|
421
|
+
calls += 1;
|
|
422
|
+
const abort = (): void => handle.cancel(callId, "The page went away.");
|
|
423
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
424
|
+
let ended: CallLine | undefined;
|
|
425
|
+
void outcome.then(
|
|
426
|
+
(result) => {
|
|
427
|
+
ended = tooLarge
|
|
428
|
+
? {
|
|
429
|
+
end: {
|
|
430
|
+
status: "failed",
|
|
431
|
+
error: {
|
|
432
|
+
code: "MOSTRA_ACTION_TOO_LARGE",
|
|
433
|
+
message: "The call's result grew past 16 MiB; the call was stopped.",
|
|
434
|
+
},
|
|
435
|
+
},
|
|
436
|
+
}
|
|
437
|
+
: result.status === "succeeded"
|
|
438
|
+
? { end: { status: "succeeded" } }
|
|
439
|
+
: result.status === "cancelled"
|
|
440
|
+
? {
|
|
441
|
+
end: {
|
|
442
|
+
status: "failed",
|
|
443
|
+
error: { code: "MOSTRA_ACTION_CANCELLED", message: "The call was cancelled." },
|
|
444
|
+
},
|
|
445
|
+
}
|
|
446
|
+
: {
|
|
447
|
+
end: {
|
|
448
|
+
status: "failed",
|
|
449
|
+
error: redactError({
|
|
450
|
+
message: result.error.message,
|
|
451
|
+
code: result.error.code ?? "MOSTRA_ACTION_THREW",
|
|
452
|
+
}),
|
|
453
|
+
},
|
|
454
|
+
};
|
|
455
|
+
wake?.();
|
|
456
|
+
},
|
|
457
|
+
(error: unknown) => {
|
|
458
|
+
const diagnostic = error instanceof RpcError ? error.diagnostic : undefined;
|
|
459
|
+
ended = {
|
|
460
|
+
end: {
|
|
461
|
+
status: "failed",
|
|
462
|
+
error: {
|
|
463
|
+
code: diagnostic?.code ?? "MOSTRA_RUNTIME_WORKER_CRASHED",
|
|
464
|
+
message: redact(diagnostic?.message ?? String(error)),
|
|
465
|
+
},
|
|
466
|
+
},
|
|
467
|
+
};
|
|
468
|
+
wake?.();
|
|
469
|
+
},
|
|
470
|
+
);
|
|
471
|
+
try {
|
|
472
|
+
for (;;) {
|
|
473
|
+
if (queue.length > 0) {
|
|
474
|
+
yield queue.shift() as CallLine;
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
if (ended) {
|
|
478
|
+
yield ended;
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
await new Promise<void>((resolve) => {
|
|
482
|
+
wake = resolve;
|
|
483
|
+
});
|
|
484
|
+
wake = undefined;
|
|
485
|
+
}
|
|
486
|
+
} finally {
|
|
487
|
+
calls -= 1;
|
|
488
|
+
signal.removeEventListener("abort", abort);
|
|
489
|
+
// A reader that stopped early (the response failed) leaves nothing running.
|
|
490
|
+
if (!ended) handle.cancel(callId, "The page went away.");
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const bridge: Actions["bridge"] = {
|
|
495
|
+
async request(worker, method, params, call) {
|
|
496
|
+
const runId = call?.context.runId;
|
|
497
|
+
switch (method) {
|
|
498
|
+
case WORKER_METHODS.secret: {
|
|
499
|
+
const { name } = ServerSecretParams.parse(params);
|
|
500
|
+
return { value: await options.secrets.resolve(worker.artifactId, name) };
|
|
501
|
+
}
|
|
502
|
+
case WORKER_METHODS.signal: {
|
|
503
|
+
const parsed = ServerSignalParams.parse(params);
|
|
504
|
+
return {
|
|
505
|
+
cursor: interaction.serverSignal(worker.artifactId, {
|
|
506
|
+
name: parsed.name,
|
|
507
|
+
...(parsed.payload !== undefined ? { payload: parsed.payload } : {}),
|
|
508
|
+
...(runId !== undefined ? { runId } : {}),
|
|
509
|
+
...(call?.viewerId !== undefined ? { viewerId: call.viewerId } : {}),
|
|
510
|
+
}),
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
case WORKER_METHODS.emit: {
|
|
514
|
+
const parsed = ServerEmitParams.parse(params);
|
|
515
|
+
if (!/^host\.[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(parsed.type)) {
|
|
516
|
+
throw failed(
|
|
517
|
+
"MOSTRA_ACTION_EVENT_INVALID",
|
|
518
|
+
`${parsed.type.slice(0, 80)} isn't a host event type.`,
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
return {
|
|
522
|
+
cursor: interaction.serverEmit(worker.artifactId, {
|
|
523
|
+
type: parsed.type,
|
|
524
|
+
...(parsed.payload !== undefined ? { payload: parsed.payload } : {}),
|
|
525
|
+
...(runId !== undefined ? { runId } : {}),
|
|
526
|
+
...(call?.viewerId !== undefined ? { viewerId: call.viewerId } : {}),
|
|
527
|
+
}),
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
case WORKER_METHODS.stateGet: {
|
|
531
|
+
const parsed = ServerStateGetParams.parse(params);
|
|
532
|
+
return interaction.serverState(worker.artifactId, parsed.ids);
|
|
533
|
+
}
|
|
534
|
+
case WORKER_METHODS.statePatch: {
|
|
535
|
+
const parsed = ServerStatePatchParams.parse(params);
|
|
536
|
+
return interaction.serverPatch(worker.artifactId, {
|
|
537
|
+
ops: redactValue(parsed.ops, options.redactor),
|
|
538
|
+
...(parsed.mutationId !== undefined ? { mutationId: parsed.mutationId } : {}),
|
|
539
|
+
...(parsed.baseRevision !== undefined ? { baseRevision: parsed.baseRevision } : {}),
|
|
540
|
+
...(runId !== undefined ? { runId } : {}),
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
default:
|
|
544
|
+
throw failed("MOSTRA_HOST_METHOD_UNKNOWN", `Mostra serves workers no method ${method}.`);
|
|
545
|
+
}
|
|
546
|
+
},
|
|
547
|
+
log(worker, line) {
|
|
548
|
+
log.info("worker.log", { versionId: worker.versionId, line: redact(line) });
|
|
549
|
+
},
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
return {
|
|
553
|
+
async call(page, input, signal) {
|
|
554
|
+
checkArgs(input.args);
|
|
555
|
+
const versionId = interaction.viewVersion(page, input.viewId);
|
|
556
|
+
const handle = await pool.acquire(functionOf(versionId, input.fnId));
|
|
557
|
+
return stream(page, input, signal, handle);
|
|
558
|
+
},
|
|
559
|
+
|
|
560
|
+
startRun(page, input) {
|
|
561
|
+
checkArgs(input.args);
|
|
562
|
+
const versionId = interaction.viewVersion(page, input.viewId);
|
|
563
|
+
functionOf(versionId, input.fnId);
|
|
564
|
+
const { state, row, started } = interaction.startRun(page, input);
|
|
565
|
+
bus.attachRuns(page.artifactId, input.viewId, [state.runId]);
|
|
566
|
+
if (started) void execute(row);
|
|
567
|
+
return state;
|
|
568
|
+
},
|
|
569
|
+
|
|
570
|
+
attach(page, input) {
|
|
571
|
+
const states = interaction.runsFor(page, input);
|
|
572
|
+
bus.attachRuns(
|
|
573
|
+
page.artifactId,
|
|
574
|
+
input.viewId,
|
|
575
|
+
states.map((state) => state.runId),
|
|
576
|
+
);
|
|
577
|
+
return states;
|
|
578
|
+
},
|
|
579
|
+
|
|
580
|
+
cancel(page, viewId, runId) {
|
|
581
|
+
const row = interaction.runFor(page, viewId, runId);
|
|
582
|
+
const running = executing.get(row.id);
|
|
583
|
+
if (running) {
|
|
584
|
+
running.cancelled = true;
|
|
585
|
+
if (running.handle && running.callId) {
|
|
586
|
+
running.handle.cancel(running.callId, "Cancelled from a page.");
|
|
587
|
+
} else running.endNow?.();
|
|
588
|
+
} else if (row.status === "queued" || row.status === "running" || row.status === "waiting") {
|
|
589
|
+
// Recorded but not in this instance (it restarted): nothing runs it, so it ends here.
|
|
590
|
+
interaction.endRun(row.id, { status: "cancelled", reason: "cancelled" });
|
|
591
|
+
}
|
|
592
|
+
return interaction.runState(row.id) as RunState;
|
|
593
|
+
},
|
|
594
|
+
|
|
595
|
+
bridge,
|
|
596
|
+
|
|
597
|
+
crashed(crash) {
|
|
598
|
+
// The runs' own `execute` ends them, flushing what they logged first; this only says why.
|
|
599
|
+
for (const callId of crash.callIds) {
|
|
600
|
+
const runId = callRuns.get(callId);
|
|
601
|
+
if (runId) crashCauses.set(runId, crash.cause);
|
|
602
|
+
}
|
|
603
|
+
},
|
|
604
|
+
|
|
605
|
+
recover() {
|
|
606
|
+
for (const row of interaction.openRuns()) {
|
|
607
|
+
interaction.endRun(row.id, {
|
|
608
|
+
status: "failed",
|
|
609
|
+
reason: "interrupted",
|
|
610
|
+
error: {
|
|
611
|
+
code: "MOSTRA_ACTION_INTERRUPTED",
|
|
612
|
+
message: "The run was interrupted: Mostra stopped while it ran.",
|
|
613
|
+
},
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
},
|
|
617
|
+
|
|
618
|
+
activity() {
|
|
619
|
+
return executing.size + calls;
|
|
620
|
+
},
|
|
621
|
+
};
|
|
622
|
+
}
|