@mostra/runtime 0.0.0-placeholder → 0.1.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 +38 -1
- 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-D7_UVdoN.js +15752 -0
- package/dist/instance-D7_UVdoN.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -4
- 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 +739 -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/index.ts +1209 -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 +942 -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 +1009 -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 +173 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +583 -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 +509 -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,942 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The instance (D45, D85, D87): the composition root. It assembles the home, the ledger, the
|
|
3
|
+
* process manager and the protocol server in their start-up order and stops them in reverse. It
|
|
4
|
+
* holds no policy of its own; each rule lives in the module that owns it.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const started = await startInstance({ mode: "daemon", home: "/path/to/home" });
|
|
8
|
+
* if (started.kind === "attached") console.log(`already running as ${started.record.pid}`);
|
|
9
|
+
* else await started.instance.stopped;
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
13
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
14
|
+
import type { Readable, Writable } from "node:stream";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import {
|
|
17
|
+
ARTIFACT_PROGRESS,
|
|
18
|
+
BROWSER_PROTOCOL_REVISIONS,
|
|
19
|
+
type ClientInfo,
|
|
20
|
+
DEV_NOTIFY,
|
|
21
|
+
type Diagnostic,
|
|
22
|
+
EVENTS_NOTIFY,
|
|
23
|
+
type InstanceInfo,
|
|
24
|
+
type JsonRpcParams,
|
|
25
|
+
type MostraInitializeResult,
|
|
26
|
+
type PortTrustedControlsActivatedParams,
|
|
27
|
+
portSecretsGetMethod,
|
|
28
|
+
portTrustedControlsActivatedMethod,
|
|
29
|
+
portTrustedControlsPolicyMethod,
|
|
30
|
+
RpcError,
|
|
31
|
+
} from "@mostra/protocol";
|
|
32
|
+
import { createActions } from "../actions/index.ts";
|
|
33
|
+
import { createAddressing, resolveBind } from "../addressing/index.ts";
|
|
34
|
+
import {
|
|
35
|
+
coverageMatcher,
|
|
36
|
+
createSpaRenderMode,
|
|
37
|
+
type RenderMode,
|
|
38
|
+
readFieldCoverage,
|
|
39
|
+
readLiveSources,
|
|
40
|
+
readServerFunctions,
|
|
41
|
+
} from "../builder/index.ts";
|
|
42
|
+
import {
|
|
43
|
+
createDependencies,
|
|
44
|
+
type Dependencies,
|
|
45
|
+
locateWarmSet,
|
|
46
|
+
recoverDependencies,
|
|
47
|
+
runtimeRoot,
|
|
48
|
+
} from "../deps/index.ts";
|
|
49
|
+
import { createDev, type Dev, recoverDev } from "../dev/index.ts";
|
|
50
|
+
import { faultsFrom } from "../faults.ts";
|
|
51
|
+
import { Gate } from "../gate.ts";
|
|
52
|
+
import {
|
|
53
|
+
Capabilities,
|
|
54
|
+
CONTROLS_POLICY_TIMEOUT_MS,
|
|
55
|
+
capabilityKey,
|
|
56
|
+
controlsPolicy,
|
|
57
|
+
createGateway,
|
|
58
|
+
type Gateway,
|
|
59
|
+
locateShell,
|
|
60
|
+
} from "../gateway/index.ts";
|
|
61
|
+
import { createGc, gcMethods } from "../gc/index.ts";
|
|
62
|
+
import {
|
|
63
|
+
acquireHome,
|
|
64
|
+
checkPlatform,
|
|
65
|
+
type InstanceRecord,
|
|
66
|
+
markHostConfigured,
|
|
67
|
+
type OwnedHome,
|
|
68
|
+
platformName,
|
|
69
|
+
readHomeSettings,
|
|
70
|
+
resolveHomePath,
|
|
71
|
+
writeHomeSettings,
|
|
72
|
+
} from "../home/index.ts";
|
|
73
|
+
import {
|
|
74
|
+
createEphemeralBus,
|
|
75
|
+
createInteraction,
|
|
76
|
+
type Interaction,
|
|
77
|
+
redactEvents,
|
|
78
|
+
} from "../interaction/index.ts";
|
|
79
|
+
import { createLive, type LatestLive, liveMethods } from "../live/index.ts";
|
|
80
|
+
import type { Environment, Log } from "../log.ts";
|
|
81
|
+
import {
|
|
82
|
+
artifactEnvironment,
|
|
83
|
+
openProcessManager,
|
|
84
|
+
type ProcessManager,
|
|
85
|
+
type ReapedChild,
|
|
86
|
+
} from "../processes/index.ts";
|
|
87
|
+
import { ProtocolServer, sessionMethods } from "../protocol-server/index.ts";
|
|
88
|
+
import {
|
|
89
|
+
artifactMethods,
|
|
90
|
+
canonicalRef,
|
|
91
|
+
createPublisher,
|
|
92
|
+
instanceChecks,
|
|
93
|
+
type Publisher,
|
|
94
|
+
versionDamage,
|
|
95
|
+
} from "../publish/index.ts";
|
|
96
|
+
import { openRegistry } from "../registry/index.ts";
|
|
97
|
+
import { openSecrets, secretMethods } from "../secrets/index.ts";
|
|
98
|
+
import { hasLoginService, type IdleWatch, watchIdle } from "../service/index.ts";
|
|
99
|
+
import {
|
|
100
|
+
collectVerifyCaches,
|
|
101
|
+
createRunnerClient,
|
|
102
|
+
createVerifier,
|
|
103
|
+
locateRunner,
|
|
104
|
+
type RunnerClient,
|
|
105
|
+
recoverVerify,
|
|
106
|
+
type SandboxOptions,
|
|
107
|
+
type Verifier,
|
|
108
|
+
} from "../verify/index.ts";
|
|
109
|
+
import {
|
|
110
|
+
createWorkerPool,
|
|
111
|
+
localLauncher,
|
|
112
|
+
locateWorker,
|
|
113
|
+
socketLauncher,
|
|
114
|
+
type WorkerLauncher,
|
|
115
|
+
type WorkerVersion,
|
|
116
|
+
workerLimits,
|
|
117
|
+
} from "../workers/index.ts";
|
|
118
|
+
|
|
119
|
+
/** Options for {@link startInstance}. */
|
|
120
|
+
export interface StartInstanceOptions {
|
|
121
|
+
/** `host`: serve `stdio` and live as long as it; `daemon`: serve the socket until stopped. */
|
|
122
|
+
readonly mode: "host" | "daemon";
|
|
123
|
+
/** `--home`. */
|
|
124
|
+
readonly home?: string | undefined;
|
|
125
|
+
/** The environment: `MOSTRA_HOME`, `MOSTRA_RUNTIME_DIR`, XDG variables. Default `process.env`. */
|
|
126
|
+
readonly env?: Environment;
|
|
127
|
+
/** The host's connection, required in `host` mode. */
|
|
128
|
+
readonly stdio?: { readonly input: Readable; readonly output: Writable };
|
|
129
|
+
/** How long a starter waits for a locked home's owner. Default 20 s. */
|
|
130
|
+
readonly waitMs?: number;
|
|
131
|
+
/** How often the socket and the home are checked, in ms (at most 10 s). Default 5 s. */
|
|
132
|
+
readonly checkIntervalMs?: number;
|
|
133
|
+
/** Where warnings and errors are mirrored besides the log, such as stderr. */
|
|
134
|
+
readonly mirror?: (line: string) => void;
|
|
135
|
+
/** `--bind`: the address the gateway listens on; `127.0.0.1` when absent (D99). */
|
|
136
|
+
readonly bind?: string | undefined;
|
|
137
|
+
/** `--url-host`: the host name URLs use; required with a wildcard `--bind` (D99). */
|
|
138
|
+
readonly urlHost?: string | undefined;
|
|
139
|
+
/**
|
|
140
|
+
* `--always-on`: a daemon that never exits for lack of work, as the login service runs it
|
|
141
|
+
* (D62). Without it a daemon exits after 30 idle minutes.
|
|
142
|
+
*/
|
|
143
|
+
readonly alwaysOn?: boolean;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** How a stopped instance ended. */
|
|
147
|
+
export interface InstanceStop {
|
|
148
|
+
/** Why it stopped. */
|
|
149
|
+
readonly reason: string;
|
|
150
|
+
/** 0 for a requested stop; 3 when the home disappeared; 1 for an unexpected failure. */
|
|
151
|
+
readonly exitCode: number;
|
|
152
|
+
/** The diagnostic, when it stopped because of one. */
|
|
153
|
+
readonly diagnostic?: Diagnostic;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** A running instance that owns its home. */
|
|
157
|
+
export interface Instance {
|
|
158
|
+
/** What `mostra/initialize` reports. */
|
|
159
|
+
readonly info: InstanceInfo;
|
|
160
|
+
/** The control socket clients attach to. */
|
|
161
|
+
readonly socketPath: string;
|
|
162
|
+
/** The process manager every child goes through. */
|
|
163
|
+
readonly processes: ProcessManager;
|
|
164
|
+
/** What start-up reaped from an earlier supervisor of this home. */
|
|
165
|
+
readonly reaped: readonly ReapedChild[];
|
|
166
|
+
/** Resolves when the instance has stopped and released its home. */
|
|
167
|
+
readonly stopped: Promise<InstanceStop>;
|
|
168
|
+
/** Stops the instance (D87). Idempotent; resolves with the first stop's outcome. */
|
|
169
|
+
stop(reason: string, exitCode?: number, diagnostic?: Diagnostic): Promise<InstanceStop>;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The outcome of {@link startInstance}. */
|
|
173
|
+
export type StartedInstance =
|
|
174
|
+
| { readonly kind: "owner"; readonly instance: Instance }
|
|
175
|
+
| {
|
|
176
|
+
readonly kind: "attached";
|
|
177
|
+
/** The instance that owns the home. */
|
|
178
|
+
readonly record: InstanceRecord;
|
|
179
|
+
readonly result: MostraInitializeResult;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
let version: string | undefined;
|
|
183
|
+
/** This runtime's release, from its package.json. */
|
|
184
|
+
export function runtimeVersion(): string {
|
|
185
|
+
if (version !== undefined) return version;
|
|
186
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
187
|
+
for (;;) {
|
|
188
|
+
try {
|
|
189
|
+
const manifest = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")) as {
|
|
190
|
+
name?: string;
|
|
191
|
+
version?: string;
|
|
192
|
+
};
|
|
193
|
+
if (manifest.name === "@mostra/runtime" && manifest.version) {
|
|
194
|
+
version = manifest.version;
|
|
195
|
+
return version;
|
|
196
|
+
}
|
|
197
|
+
} catch {
|
|
198
|
+
// Keep looking upwards.
|
|
199
|
+
}
|
|
200
|
+
const parent = dirname(dir);
|
|
201
|
+
if (parent === dir) throw new Error("Couldn't find @mostra/runtime's package.json.");
|
|
202
|
+
dir = parent;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const CLIENT: ClientInfo = { name: "mostra-runtime", version: "" };
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Starts an instance for a home (RT-06): the platform gate, then the home's lock, migrations,
|
|
210
|
+
* reaping, the socket and `instance.json`. A daemon starter that finds the home owned attaches
|
|
211
|
+
* to the owner instead. Throws an `RpcError` carrying the diagnostic when it can do neither.
|
|
212
|
+
*/
|
|
213
|
+
export async function startInstance(options: StartInstanceOptions): Promise<StartedInstance> {
|
|
214
|
+
const gate = checkPlatform();
|
|
215
|
+
if (gate) throw new RpcError(gate);
|
|
216
|
+
if (options.mode === "host" && !options.stdio) throw new Error("host mode needs stdio.");
|
|
217
|
+
// Wrong bind flags fail before the home is touched (D99).
|
|
218
|
+
resolveBind({ bind: options.bind, urlHost: options.urlHost });
|
|
219
|
+
const env = options.env ?? process.env;
|
|
220
|
+
const acquisition = await acquireHome({
|
|
221
|
+
home: resolveHomePath({ home: options.home, env }),
|
|
222
|
+
env,
|
|
223
|
+
mode: options.mode,
|
|
224
|
+
runtimeVersion: runtimeVersion(),
|
|
225
|
+
client: { ...CLIENT, version: runtimeVersion() },
|
|
226
|
+
...(options.waitMs !== undefined ? { waitMs: options.waitMs } : {}),
|
|
227
|
+
...(options.mirror ? { mirror: options.mirror } : {}),
|
|
228
|
+
});
|
|
229
|
+
if (acquisition.kind === "attached") {
|
|
230
|
+
acquisition.connection.close();
|
|
231
|
+
return { kind: "attached", record: acquisition.record, result: acquisition.result };
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
return { kind: "owner", instance: await assemble(acquisition.home, options) };
|
|
235
|
+
} catch (error) {
|
|
236
|
+
acquisition.home.release();
|
|
237
|
+
throw error;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function sameHome(home: string, path: string): boolean {
|
|
242
|
+
try {
|
|
243
|
+
return realpathSync(path) === home;
|
|
244
|
+
} catch {
|
|
245
|
+
return resolve(path) === home;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function assemble(home: OwnedHome, options: StartInstanceOptions): Promise<Instance> {
|
|
250
|
+
const { log } = home;
|
|
251
|
+
const environment = options.env ?? process.env;
|
|
252
|
+
const faults = faultsFrom(environment);
|
|
253
|
+
const { manager, reaped } = await openProcessManager({
|
|
254
|
+
ledger: home.ledger,
|
|
255
|
+
log,
|
|
256
|
+
redactor: home.redactor,
|
|
257
|
+
faults,
|
|
258
|
+
});
|
|
259
|
+
const registry = await openRegistry({ home: home.path, ledger: home.ledger });
|
|
260
|
+
const recovered = await registry.recover();
|
|
261
|
+
if (recovered.versions.length > 0 || recovered.snapshots.length > 0) {
|
|
262
|
+
log.info("publish.recovered", { ...recovered });
|
|
263
|
+
}
|
|
264
|
+
await recoverDependencies({ home: home.path, log });
|
|
265
|
+
// What a stopped instance left of verify: sandboxes, unfinished builds, orphan files (D138 item 1).
|
|
266
|
+
await recoverVerify(home.path, log);
|
|
267
|
+
// And of `mostra dev`: its sessions' mirrors, builds and scratch ledgers (D140 item 12).
|
|
268
|
+
await recoverDev(home.path, log);
|
|
269
|
+
// Building and serving go through the render mode (D19 seam 5): SPA, v1's only one. Made on
|
|
270
|
+
// first use, like the publisher, so a runtime missing its build packages still starts.
|
|
271
|
+
let spa: RenderMode | undefined;
|
|
272
|
+
const renderMode = (): RenderMode => {
|
|
273
|
+
spa ??= createSpaRenderMode({
|
|
274
|
+
processes: manager,
|
|
275
|
+
log,
|
|
276
|
+
env: environment,
|
|
277
|
+
faults,
|
|
278
|
+
// Tests shorten the 180 s build limit to exercise MOSTRA_BUILD_TIMEOUT.
|
|
279
|
+
...(Number(environment["MOSTRA_TEST_BUILD_TIMEOUT_MS"]) > 0
|
|
280
|
+
? { timeoutMs: Number(environment["MOSTRA_TEST_BUILD_TIMEOUT_MS"]) }
|
|
281
|
+
: {}),
|
|
282
|
+
});
|
|
283
|
+
return spa;
|
|
284
|
+
};
|
|
285
|
+
// Built on first use: a runtime without its warm set still starts and says what's missing.
|
|
286
|
+
let dependencies: Dependencies | undefined;
|
|
287
|
+
// gc runs alone; publishes, verify runs and installs share the gate (D140 item 14).
|
|
288
|
+
const gate = new Gate();
|
|
289
|
+
const getDependencies = (): Dependencies =>
|
|
290
|
+
(dependencies ??= createDependencies({
|
|
291
|
+
gate,
|
|
292
|
+
home: home.path,
|
|
293
|
+
ledger: home.ledger,
|
|
294
|
+
processes: manager,
|
|
295
|
+
log,
|
|
296
|
+
env: environment,
|
|
297
|
+
warm: locateWarmSet(environment),
|
|
298
|
+
policy: () => readHomeSettings(home.path).dependencies ?? "auto",
|
|
299
|
+
faults,
|
|
300
|
+
// Tests shorten the 300 s install limit to exercise MOSTRA_DEP_TIMEOUT.
|
|
301
|
+
...(Number(environment["MOSTRA_TEST_INSTALL_TIMEOUT_MS"]) > 0
|
|
302
|
+
? { installTimeoutMs: Number(environment["MOSTRA_TEST_INSTALL_TIMEOUT_MS"]) }
|
|
303
|
+
: {}),
|
|
304
|
+
}));
|
|
305
|
+
// Suites of other features publish without verify's gate; the switch is loud (D138).
|
|
306
|
+
const unverifiedPublish = Boolean(environment["MOSTRA_TEST_UNVERIFIED_PUBLISH"]);
|
|
307
|
+
if (unverifiedPublish) log.warn("instance.test_unverified_publish", {});
|
|
308
|
+
let publisher: Publisher | undefined;
|
|
309
|
+
const getPublisher = (): Publisher => {
|
|
310
|
+
if (publisher) return publisher;
|
|
311
|
+
publisher = createPublisher({
|
|
312
|
+
gate,
|
|
313
|
+
registry,
|
|
314
|
+
ledger: home.ledger,
|
|
315
|
+
dependencies: getDependencies(),
|
|
316
|
+
renderMode: renderMode(),
|
|
317
|
+
runtimeVersion: runtimeVersion(),
|
|
318
|
+
log,
|
|
319
|
+
faults,
|
|
320
|
+
homePolicy: () => readHomeSettings(home.path).submission,
|
|
321
|
+
verifier: getVerifier,
|
|
322
|
+
unverified: unverifiedPublish,
|
|
323
|
+
});
|
|
324
|
+
return publisher;
|
|
325
|
+
};
|
|
326
|
+
// verify (WP-09, D138): the runner child with its warm browser, and the sandbox each run serves
|
|
327
|
+
// its build from. Made on first use, like the publisher.
|
|
328
|
+
const revisions = servedRevisions(environment["MOSTRA_TEST_BROWSER_REVISION_NEXT"], log);
|
|
329
|
+
let runner: RunnerClient | undefined;
|
|
330
|
+
let verifier: Verifier | undefined;
|
|
331
|
+
// What a sandbox needs of the instance: verify's runs and `mostra dev`'s sessions (D138, D140).
|
|
332
|
+
const sandboxOptions = (): SandboxOptions => {
|
|
333
|
+
const workerLocation = locateWorker(environment, runtimeRoot());
|
|
334
|
+
return {
|
|
335
|
+
home: home.path,
|
|
336
|
+
log,
|
|
337
|
+
redactor: home.redactor,
|
|
338
|
+
processes: manager,
|
|
339
|
+
runtimeVersion: runtimeVersion(),
|
|
340
|
+
worker: { entry: workerLocation.entry, nodeArgs: workerLocation.nodeArgs },
|
|
341
|
+
modules: (target) => getDependencies().runtimeModules(target),
|
|
342
|
+
secrets,
|
|
343
|
+
env: artifactEnvironment(environment),
|
|
344
|
+
shellDir: locateShell(environment),
|
|
345
|
+
revisions,
|
|
346
|
+
serverEntry: (dir) => renderMode().serverEntry(dir),
|
|
347
|
+
damage: (dir) => versionDamage(dir),
|
|
348
|
+
};
|
|
349
|
+
};
|
|
350
|
+
const getVerifier = (): Verifier => {
|
|
351
|
+
if (verifier) return verifier;
|
|
352
|
+
const located = locateRunner(environment, runtimeRoot());
|
|
353
|
+
runner = createRunnerClient({
|
|
354
|
+
processes: manager,
|
|
355
|
+
log,
|
|
356
|
+
env: environment,
|
|
357
|
+
entry: located.entry,
|
|
358
|
+
nodeArgs: located.nodeArgs,
|
|
359
|
+
browsers: located.browsers,
|
|
360
|
+
});
|
|
361
|
+
verifier = createVerifier({
|
|
362
|
+
home: home.path,
|
|
363
|
+
ledger: home.ledger,
|
|
364
|
+
registry,
|
|
365
|
+
log,
|
|
366
|
+
redactor: home.redactor,
|
|
367
|
+
runner,
|
|
368
|
+
sandbox: sandboxOptions(),
|
|
369
|
+
// Tests shorten verify's 120 s and its smoke test's 200 interactions.
|
|
370
|
+
limits: {
|
|
371
|
+
...(Number(environment["MOSTRA_TEST_VERIFY_TIMEOUT_MS"]) > 0
|
|
372
|
+
? { timeoutMs: Number(environment["MOSTRA_TEST_VERIFY_TIMEOUT_MS"]) }
|
|
373
|
+
: {}),
|
|
374
|
+
...(Number(environment["MOSTRA_TEST_SMOKE_INTERACTIONS"]) > 0
|
|
375
|
+
? { smokeInteractions: Number(environment["MOSTRA_TEST_SMOKE_INTERACTIONS"]) }
|
|
376
|
+
: {}),
|
|
377
|
+
},
|
|
378
|
+
// verify clears the artifact's park, for its calls and its live producers (D133).
|
|
379
|
+
clearPark: (artifactId) => {
|
|
380
|
+
pool.clear(artifactId);
|
|
381
|
+
live.unpark(artifactId);
|
|
382
|
+
},
|
|
383
|
+
});
|
|
384
|
+
return verifier;
|
|
385
|
+
};
|
|
386
|
+
// The gateway (WP-05): leased ports, capability links and sessions, static versions.
|
|
387
|
+
const clockOffset = testClockOffset(environment["MOSTRA_TEST_CLOCK_OFFSET_MS"], log);
|
|
388
|
+
const addressing = createAddressing({
|
|
389
|
+
ledger: home.ledger,
|
|
390
|
+
log,
|
|
391
|
+
bind: resolveBind({ bind: options.bind, urlHost: options.urlHost }),
|
|
392
|
+
});
|
|
393
|
+
const capabilities = new Capabilities({
|
|
394
|
+
ledger: home.ledger,
|
|
395
|
+
log,
|
|
396
|
+
key: capabilityKey(home.path, log),
|
|
397
|
+
// Tests move the gateway's clock to check link lifetimes (LL-05, HY-03).
|
|
398
|
+
now: () => Date.now() + clockOffset,
|
|
399
|
+
shellOrigin: () => gateway.shellOrigin(),
|
|
400
|
+
});
|
|
401
|
+
// The interaction core (WP-06): drafts, state, submissions, the outbox, events and waits. Its
|
|
402
|
+
// host connections are the protocol server's, built below.
|
|
403
|
+
const interaction: Interaction = createInteraction({
|
|
404
|
+
ledger: home.ledger,
|
|
405
|
+
log,
|
|
406
|
+
faults,
|
|
407
|
+
hosts: {
|
|
408
|
+
registrants: (port) => server.registrants(port),
|
|
409
|
+
call: (session, method, params, timeoutMs) =>
|
|
410
|
+
server.callSession(session, method, params as JsonRpcParams, timeoutMs),
|
|
411
|
+
notify: (session, method, params) => server.notify(session, method, params as JsonRpcParams),
|
|
412
|
+
drained: (session) => server.drained(session),
|
|
413
|
+
},
|
|
414
|
+
// The hosts that always provide the submissions port (D105).
|
|
415
|
+
expectedHosts: () =>
|
|
416
|
+
Object.entries(readHomeSettings(home.path).expectedPorts ?? {})
|
|
417
|
+
.filter(([, ports]) => ports.includes("submissions"))
|
|
418
|
+
.map(([owner]) => owner),
|
|
419
|
+
artifacts: { resolve: (ref) => registry.get(canonicalRef(ref)).id },
|
|
420
|
+
// Which field IDs each version covers (D109); versions are immutable, so it is read once.
|
|
421
|
+
coverage: (() => {
|
|
422
|
+
const cache = new Map<string, (fieldId: string) => boolean>();
|
|
423
|
+
return (versionId: string) => {
|
|
424
|
+
let matcher = cache.get(versionId);
|
|
425
|
+
if (!matcher) {
|
|
426
|
+
matcher = coverageMatcher(readFieldCoverage(registry.store.versionDir(versionId)));
|
|
427
|
+
if (cache.size >= 512) cache.delete(cache.keys().next().value as string);
|
|
428
|
+
cache.set(versionId, matcher);
|
|
429
|
+
}
|
|
430
|
+
return matcher;
|
|
431
|
+
};
|
|
432
|
+
})(),
|
|
433
|
+
// Tests shorten the 30 s port-call limit to exercise a host that never answers.
|
|
434
|
+
...(Number(environment["MOSTRA_TEST_PORT_TIMEOUT_MS"]) > 0
|
|
435
|
+
? { portTimeoutMs: Number(environment["MOSTRA_TEST_PORT_TIMEOUT_MS"]) }
|
|
436
|
+
: {}),
|
|
437
|
+
});
|
|
438
|
+
// Server code (WP-08): secrets, the worker pool, action runs and live sources. Every secret the
|
|
439
|
+
// home holds is protected from the log and the event log before anything runs.
|
|
440
|
+
const stopRedactingEvents = redactEvents(home.redactor);
|
|
441
|
+
const secrets = openSecrets({
|
|
442
|
+
home: home.path,
|
|
443
|
+
redactor: home.redactor,
|
|
444
|
+
log,
|
|
445
|
+
port: () => {
|
|
446
|
+
const registrant = server.registrants("secrets").at(-1);
|
|
447
|
+
if (!registrant) return undefined;
|
|
448
|
+
return async (request) => {
|
|
449
|
+
const value = await server.callSession(
|
|
450
|
+
registrant.session,
|
|
451
|
+
portSecretsGetMethod.method,
|
|
452
|
+
request as unknown as JsonRpcParams,
|
|
453
|
+
PORT_TIMEOUT_MS,
|
|
454
|
+
);
|
|
455
|
+
return typeof value === "string" ? value : null;
|
|
456
|
+
};
|
|
457
|
+
},
|
|
458
|
+
});
|
|
459
|
+
// Tests shorten the one-minute lease a page's interest in a run or live source holds.
|
|
460
|
+
const testLease = Number(environment["MOSTRA_TEST_INTEREST_LEASE_MS"]);
|
|
461
|
+
const bus = createEphemeralBus(testLease > 0 ? { leaseMs: testLease } : {});
|
|
462
|
+
// The local launcher forks workers from this home's versions; tests prove the seam with a worker
|
|
463
|
+
// host started on its own, reached over a socket (D124).
|
|
464
|
+
const workerSocket = environment["MOSTRA_TEST_WORKER_HOST"];
|
|
465
|
+
const located = locateWorker(environment, runtimeRoot());
|
|
466
|
+
const launcher: WorkerLauncher = workerSocket
|
|
467
|
+
? socketLauncher(workerSocket)
|
|
468
|
+
: localLauncher({
|
|
469
|
+
processes: manager,
|
|
470
|
+
home: home.path,
|
|
471
|
+
workerEntry: located.entry,
|
|
472
|
+
nodeArgs: located.nodeArgs,
|
|
473
|
+
// A version's packages run from the layout it was built against (D135).
|
|
474
|
+
modules: (target) => getDependencies().runtimeModules(target),
|
|
475
|
+
});
|
|
476
|
+
if (workerSocket) log.warn("workers.test_host", { socket: workerSocket });
|
|
477
|
+
const workerVersions = new Map<string, WorkerVersion | null>();
|
|
478
|
+
/** What a version's worker needs, or `null` for a version without server code (RT-05). */
|
|
479
|
+
const workerVersion = (versionId: string): WorkerVersion | undefined => {
|
|
480
|
+
const cached = workerVersions.get(versionId);
|
|
481
|
+
if (cached !== undefined) return cached ?? undefined;
|
|
482
|
+
const version = registry.version(versionId);
|
|
483
|
+
const dir = registry.store.versionDir(versionId);
|
|
484
|
+
// The render mode says what a worker loads from a version (D19 seam 5).
|
|
485
|
+
const served = version ? renderMode().serverEntry(dir) : undefined;
|
|
486
|
+
const entry = served === undefined ? undefined : relative(dir, served);
|
|
487
|
+
if (!version || entry === undefined) {
|
|
488
|
+
if (version) workerVersions.set(versionId, null);
|
|
489
|
+
return undefined;
|
|
490
|
+
}
|
|
491
|
+
const source = registry.sourceOf(version.artifactId);
|
|
492
|
+
const sourceDir = source ? sourceDirectory(source) : undefined;
|
|
493
|
+
const resolved: WorkerVersion = {
|
|
494
|
+
artifactId: version.artifactId,
|
|
495
|
+
ref: { versionId, outputDigest: version.outputDigest, locator: `home:versions/${versionId}` },
|
|
496
|
+
entry,
|
|
497
|
+
limits: workerLimits(version.manifest),
|
|
498
|
+
functions: readServerFunctions(dir),
|
|
499
|
+
...(sourceDir ? { sourceDir } : {}),
|
|
500
|
+
};
|
|
501
|
+
workerVersions.set(versionId, resolved);
|
|
502
|
+
return resolved;
|
|
503
|
+
};
|
|
504
|
+
const testIdleMs = Number(environment["MOSTRA_TEST_WORKER_IDLE_MS"]);
|
|
505
|
+
const pool = createWorkerPool({
|
|
506
|
+
launcher,
|
|
507
|
+
log,
|
|
508
|
+
// The artifact's allowlisted environment (D95), never the supervisor's secrets (D124).
|
|
509
|
+
env: artifactEnvironment(environment),
|
|
510
|
+
bridge: {
|
|
511
|
+
request: (worker, method, params, call) =>
|
|
512
|
+
actions.bridge.request(worker, method, params, call),
|
|
513
|
+
log: (worker, line) => actions.bridge.log(worker, line),
|
|
514
|
+
liveValue: (worker, params) => live.produced(worker, params),
|
|
515
|
+
},
|
|
516
|
+
onCrash: (crash) => {
|
|
517
|
+
actions.crashed(crash);
|
|
518
|
+
live.crashed(crash);
|
|
519
|
+
try {
|
|
520
|
+
interaction.workerCrashed(crash);
|
|
521
|
+
} catch (error) {
|
|
522
|
+
log.error("workers.crash_record_failed", { error });
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
...(testIdleMs > 0 ? { idleMsOverride: testIdleMs } : {}),
|
|
526
|
+
// Tests shorten the 1 s first backoff to crash a worker into its park quickly.
|
|
527
|
+
...(Number(environment["MOSTRA_TEST_WORKER_BACKOFF_MS"]) > 0
|
|
528
|
+
? { backoffMinMs: Number(environment["MOSTRA_TEST_WORKER_BACKOFF_MS"]) }
|
|
529
|
+
: {}),
|
|
530
|
+
});
|
|
531
|
+
const actions = createActions({
|
|
532
|
+
interaction,
|
|
533
|
+
pool,
|
|
534
|
+
bus,
|
|
535
|
+
secrets,
|
|
536
|
+
redactor: home.redactor,
|
|
537
|
+
log,
|
|
538
|
+
version: workerVersion,
|
|
539
|
+
});
|
|
540
|
+
// Versions are immutable, so each one's live sources are read from its registry once. Which
|
|
541
|
+
// version is each artifact's latest is read from the ledger once, then kept up to date from
|
|
542
|
+
// `version.published`, so a reconcile costs nothing per artifact without live sources, however
|
|
543
|
+
// many the home holds (D132; Opus review 2).
|
|
544
|
+
const liveSourcesOf = new Map<string, ReturnType<typeof readLiveSources>>();
|
|
545
|
+
const sourcesOf = (versionId: string): ReturnType<typeof readLiveSources> => {
|
|
546
|
+
let sources = liveSourcesOf.get(versionId);
|
|
547
|
+
if (!sources) {
|
|
548
|
+
sources = readLiveSources(registry.store.versionDir(versionId));
|
|
549
|
+
liveSourcesOf.set(versionId, sources);
|
|
550
|
+
}
|
|
551
|
+
return sources;
|
|
552
|
+
};
|
|
553
|
+
/** The latest version of each artifact whose latest version defines live sources. */
|
|
554
|
+
const latestWithLive = new Map<string, string>();
|
|
555
|
+
const noteLatest = (artifactId: string, versionId: string): void => {
|
|
556
|
+
if (sourcesOf(versionId).length > 0) latestWithLive.set(artifactId, versionId);
|
|
557
|
+
else latestWithLive.delete(artifactId);
|
|
558
|
+
};
|
|
559
|
+
for (const [artifactId, versionId] of registry.latestVersions())
|
|
560
|
+
noteLatest(artifactId, versionId);
|
|
561
|
+
const latestLive = (artifactId: string): LatestLive | undefined => {
|
|
562
|
+
const versionId = latestWithLive.get(artifactId);
|
|
563
|
+
return versionId ? { versionId, sources: sourcesOf(versionId) } : undefined;
|
|
564
|
+
};
|
|
565
|
+
const live = createLive({
|
|
566
|
+
ledger: home.ledger,
|
|
567
|
+
log,
|
|
568
|
+
bus,
|
|
569
|
+
pool,
|
|
570
|
+
interaction,
|
|
571
|
+
latest: latestLive,
|
|
572
|
+
redactor: home.redactor,
|
|
573
|
+
artifactsWithLive: () => [...latestWithLive.keys()],
|
|
574
|
+
version: workerVersion,
|
|
575
|
+
...(testLease > 0 ? { reconcileMs: Math.max(100, testLease / 4) } : {}),
|
|
576
|
+
});
|
|
577
|
+
// A new latest version moves its artifact's producers to it (ARCHITECTURE §7).
|
|
578
|
+
const stopWatchingPublishes = interaction.onPublished((artifactId, versionId) => {
|
|
579
|
+
noteLatest(artifactId, versionId);
|
|
580
|
+
live.reconcile();
|
|
581
|
+
});
|
|
582
|
+
// Runs a stopped instance left open end now (D24: never retried).
|
|
583
|
+
actions.recover();
|
|
584
|
+
|
|
585
|
+
const shellDir = locateShell(environment);
|
|
586
|
+
if (shellDir === undefined)
|
|
587
|
+
log.warn("gateway.shell_missing", { code: "MOSTRA_ENV_SHELL_MISSING" });
|
|
588
|
+
const gateway: Gateway = createGateway({
|
|
589
|
+
ledger: home.ledger,
|
|
590
|
+
log,
|
|
591
|
+
addressing,
|
|
592
|
+
capabilities,
|
|
593
|
+
artifacts: {
|
|
594
|
+
get: (ref) => registry.get(canonicalRef(ref)),
|
|
595
|
+
versionDir: (versionId) => registry.store.versionDir(versionId),
|
|
596
|
+
damage: (versionId) => versionDamage(registry.store.versionDir(versionId)),
|
|
597
|
+
},
|
|
598
|
+
shellDir,
|
|
599
|
+
interaction,
|
|
600
|
+
actions,
|
|
601
|
+
live,
|
|
602
|
+
bus,
|
|
603
|
+
parked: (versionId) =>
|
|
604
|
+
pool.status().some((w) => w.versionId === versionId && w.state === "parked"),
|
|
605
|
+
revisions,
|
|
606
|
+
controls: (artifact) =>
|
|
607
|
+
controlsPolicy({
|
|
608
|
+
artifact,
|
|
609
|
+
log,
|
|
610
|
+
home: () => readHomeSettings(home.path).submitChannels,
|
|
611
|
+
expected: () =>
|
|
612
|
+
Object.entries(readHomeSettings(home.path).expectedPorts ?? {})
|
|
613
|
+
.filter(([, ports]) => ports.includes("trustedControls"))
|
|
614
|
+
.map(([owner]) => owner),
|
|
615
|
+
registrant: () => server.registrants("trustedControls").at(-1),
|
|
616
|
+
call: (session, params) =>
|
|
617
|
+
server.callSession(
|
|
618
|
+
session,
|
|
619
|
+
portTrustedControlsPolicyMethod.method,
|
|
620
|
+
params as unknown as JsonRpcParams,
|
|
621
|
+
CONTROLS_POLICY_TIMEOUT_MS,
|
|
622
|
+
),
|
|
623
|
+
}),
|
|
624
|
+
activated: (event) => {
|
|
625
|
+
// Best effort and never retried, like draft mirrors (D105, D108): the change is durable.
|
|
626
|
+
for (const registrant of server.registrants("trustedControls")) {
|
|
627
|
+
const params: PortTrustedControlsActivatedParams = {
|
|
628
|
+
artifactId: event.artifactId,
|
|
629
|
+
control: event.control,
|
|
630
|
+
viewer: interaction.viewer(event.viewerId),
|
|
631
|
+
viewId: event.viewId,
|
|
632
|
+
detail: event.detail,
|
|
633
|
+
};
|
|
634
|
+
server
|
|
635
|
+
.callSession(
|
|
636
|
+
registrant.session,
|
|
637
|
+
portTrustedControlsActivatedMethod.method,
|
|
638
|
+
params as unknown as JsonRpcParams,
|
|
639
|
+
CONTROLS_POLICY_TIMEOUT_MS,
|
|
640
|
+
)
|
|
641
|
+
.catch((error: unknown) =>
|
|
642
|
+
log.warn("gateway.activated_failed", {
|
|
643
|
+
owner: registrant.owner,
|
|
644
|
+
error: (error as Error).message,
|
|
645
|
+
}),
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
},
|
|
649
|
+
});
|
|
650
|
+
let gatewayStarted = false;
|
|
651
|
+
let idle: IdleWatch | undefined;
|
|
652
|
+
const info: InstanceInfo = {
|
|
653
|
+
version: runtimeVersion(),
|
|
654
|
+
home: home.path,
|
|
655
|
+
platform: platformName(),
|
|
656
|
+
pid: process.pid,
|
|
657
|
+
mode: options.mode,
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
let stopping: Promise<InstanceStop> | undefined;
|
|
661
|
+
let markStopped: (stop: InstanceStop) => void = () => {};
|
|
662
|
+
const stopped = new Promise<InstanceStop>((done) => {
|
|
663
|
+
markStopped = done;
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
// `mostra dev`'s sessions (D140 item 12), each owned by the connection that started it.
|
|
667
|
+
const dev: Dev = createDev({
|
|
668
|
+
home: home.path,
|
|
669
|
+
log,
|
|
670
|
+
registry,
|
|
671
|
+
renderMode,
|
|
672
|
+
dependencies: getDependencies,
|
|
673
|
+
gate,
|
|
674
|
+
sandbox: sandboxOptions,
|
|
675
|
+
bindAddress: addressing.bindAddress,
|
|
676
|
+
faults,
|
|
677
|
+
notify: (session, method, params) =>
|
|
678
|
+
server.notify(session, method, params as unknown as JsonRpcParams),
|
|
679
|
+
});
|
|
680
|
+
const server: ProtocolServer = new ProtocolServer({
|
|
681
|
+
log,
|
|
682
|
+
describe: () => info,
|
|
683
|
+
servesHome: (path) => sameHome(home.path, path),
|
|
684
|
+
// A host's settings persist in the home's config.json for every later client (D95, D102,
|
|
685
|
+
// D105).
|
|
686
|
+
configureHome: (config, client) => {
|
|
687
|
+
const current = readHomeSettings(home.path);
|
|
688
|
+
const expectedPorts = { ...(current.expectedPorts ?? {}) };
|
|
689
|
+
if (config.ports !== undefined) {
|
|
690
|
+
if (config.ports.length === 0) delete expectedPorts[client.name];
|
|
691
|
+
else expectedPorts[client.name] = [...new Set(config.ports)].sort();
|
|
692
|
+
}
|
|
693
|
+
const change = {
|
|
694
|
+
...(config.dependencies !== undefined ? { dependencies: config.dependencies } : {}),
|
|
695
|
+
...(config.submission !== undefined ? { submission: config.submission } : {}),
|
|
696
|
+
...(config.ports !== undefined ? { expectedPorts } : {}),
|
|
697
|
+
...(config.submitChannels !== undefined
|
|
698
|
+
? { submitChannels: [...new Set(config.submitChannels)].sort() }
|
|
699
|
+
: {}),
|
|
700
|
+
};
|
|
701
|
+
if (Object.keys(change).length === 0) return;
|
|
702
|
+
writeHomeSettings(home.path, change);
|
|
703
|
+
log.info("home.settings_written", {
|
|
704
|
+
client: client.name,
|
|
705
|
+
...(config.dependencies !== undefined ? { dependencies: config.dependencies } : {}),
|
|
706
|
+
...(config.submission !== undefined ? { submission: config.submission } : {}),
|
|
707
|
+
...(config.ports !== undefined ? { ports: config.ports } : {}),
|
|
708
|
+
...(config.submitChannels !== undefined ? { submitChannels: config.submitChannels } : {}),
|
|
709
|
+
});
|
|
710
|
+
},
|
|
711
|
+
// A host's home never sends the standalone CLI's telemetry (D140 item 4).
|
|
712
|
+
markHostConfigured: (client) => {
|
|
713
|
+
if (markHostConfigured(home.path, client.name))
|
|
714
|
+
log.info("home.host_configured", { client: client.name });
|
|
715
|
+
},
|
|
716
|
+
supportedPorts: ["submissions", "drafts", "trustedControls", "secrets"],
|
|
717
|
+
notifications: [EVENTS_NOTIFY, ARTIFACT_PROGRESS, DEV_NOTIFY],
|
|
718
|
+
onInitialized: (session) => interaction.connected(session),
|
|
719
|
+
onClosed: (session) => interaction.disconnected(session),
|
|
720
|
+
redact: (text) => home.redactor.redact(text),
|
|
721
|
+
handlers: [
|
|
722
|
+
...artifactMethods({
|
|
723
|
+
registry,
|
|
724
|
+
publisher: getPublisher,
|
|
725
|
+
link: (artifactId) => gateway.defaultLink(artifactId),
|
|
726
|
+
// Any publish clears the artifact's park, a reused version too (FAILURE_MODEL §2): its
|
|
727
|
+
// calls and its live producers both start again.
|
|
728
|
+
published: (artifactId) => {
|
|
729
|
+
pool.clear(artifactId);
|
|
730
|
+
live.unpark(artifactId);
|
|
731
|
+
},
|
|
732
|
+
notify: (session, method, params) => {
|
|
733
|
+
server.notify(session, method, params as JsonRpcParams);
|
|
734
|
+
},
|
|
735
|
+
dependenciesAdded: (artifact) => dev.dependenciesChanged(artifact),
|
|
736
|
+
}),
|
|
737
|
+
...dev.methods(),
|
|
738
|
+
...gateway.methods(),
|
|
739
|
+
...interaction.methods(),
|
|
740
|
+
...secretMethods(secrets, (ref) => registry.get(canonicalRef(ref)).id),
|
|
741
|
+
...gcMethods(
|
|
742
|
+
createGc({
|
|
743
|
+
ledger: home.ledger,
|
|
744
|
+
registry,
|
|
745
|
+
log,
|
|
746
|
+
gate,
|
|
747
|
+
dependencies: getDependencies,
|
|
748
|
+
// Verify's caches outlive the process that wrote them: gc reads them from the home
|
|
749
|
+
// whether or not verify ran since this instance started (GPT review finding 7).
|
|
750
|
+
caches: (dryRun) =>
|
|
751
|
+
verifier ? verifier.collect(dryRun) : collectVerifyCaches(home.path, { dryRun }),
|
|
752
|
+
now: () => Date.now() + clockOffset,
|
|
753
|
+
locksInUse: () => dev.locks(),
|
|
754
|
+
faults,
|
|
755
|
+
}),
|
|
756
|
+
),
|
|
757
|
+
...liveMethods(live, (ref) => registry.get(canonicalRef(ref)).id),
|
|
758
|
+
...sessionMethods({
|
|
759
|
+
describe: () => info,
|
|
760
|
+
startedAt: home.startedAt,
|
|
761
|
+
socketPath: () => home.socketPath,
|
|
762
|
+
connections: () => server.connections,
|
|
763
|
+
processes: () => manager.list(),
|
|
764
|
+
gateway: () =>
|
|
765
|
+
gatewayStarted
|
|
766
|
+
? {
|
|
767
|
+
bind: addressing.bindAddress,
|
|
768
|
+
shell: gateway.shellOrigin(),
|
|
769
|
+
artifacts: [...gateway.artifactOrigins()].map(([artifactId, origin]) => ({
|
|
770
|
+
artifactId,
|
|
771
|
+
origin,
|
|
772
|
+
})),
|
|
773
|
+
}
|
|
774
|
+
: undefined,
|
|
775
|
+
deliveries: () => interaction.deliveries(),
|
|
776
|
+
workers: () => pool.status(),
|
|
777
|
+
idleExitAt: () => idle?.exitAt(),
|
|
778
|
+
checks: (deep) =>
|
|
779
|
+
instanceChecks({
|
|
780
|
+
deep,
|
|
781
|
+
ledger: home.ledger,
|
|
782
|
+
registry,
|
|
783
|
+
shellDir,
|
|
784
|
+
warm: () => locateWarmSet(environment),
|
|
785
|
+
}),
|
|
786
|
+
shutdown: (reason) => void instance.stop(reason, 0),
|
|
787
|
+
}),
|
|
788
|
+
],
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
const instance: Instance = {
|
|
792
|
+
info,
|
|
793
|
+
socketPath: home.socketPath,
|
|
794
|
+
processes: manager,
|
|
795
|
+
reaped,
|
|
796
|
+
stopped,
|
|
797
|
+
stop(reason, exitCode = 0, diagnostic) {
|
|
798
|
+
stopping ??= (async () => {
|
|
799
|
+
log.info("instance.stopping", { reason, exitCode });
|
|
800
|
+
idle?.stop();
|
|
801
|
+
stopWatchingPublishes();
|
|
802
|
+
live.stop();
|
|
803
|
+
interaction.stop();
|
|
804
|
+
server.close();
|
|
805
|
+
await dev.stopAll();
|
|
806
|
+
await gateway.close();
|
|
807
|
+
await pool.stopAll();
|
|
808
|
+
await runner?.close();
|
|
809
|
+
bus.close();
|
|
810
|
+
stopRedactingEvents();
|
|
811
|
+
try {
|
|
812
|
+
await manager.stopAll();
|
|
813
|
+
} catch (error) {
|
|
814
|
+
log.error("instance.children_stop_failed", { error });
|
|
815
|
+
}
|
|
816
|
+
home.release();
|
|
817
|
+
const outcome: InstanceStop = diagnostic
|
|
818
|
+
? { reason, exitCode, diagnostic }
|
|
819
|
+
: { reason, exitCode };
|
|
820
|
+
log.info("instance.stopped", { reason, exitCode });
|
|
821
|
+
markStopped(outcome);
|
|
822
|
+
return outcome;
|
|
823
|
+
})();
|
|
824
|
+
return stopping;
|
|
825
|
+
},
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
try {
|
|
829
|
+
await gateway.start();
|
|
830
|
+
gatewayStarted = true;
|
|
831
|
+
await server.listen(home.socketPath, options.checkIntervalMs);
|
|
832
|
+
// Deliveries pending from before a restart go out again (FAILURE_MODEL §4).
|
|
833
|
+
interaction.start();
|
|
834
|
+
// Producers that `always` run, and those hosts subscribe to, start now (LL-04).
|
|
835
|
+
live.start();
|
|
836
|
+
home.publish(options.mode);
|
|
837
|
+
// A host-spawned instance marks its home as a host's (D140 item 4).
|
|
838
|
+
if (options.mode === "host" && markHostConfigured(home.path, "mostra-runtime host"))
|
|
839
|
+
log.info("home.host_configured", { client: "mostra-runtime host" });
|
|
840
|
+
} catch (error) {
|
|
841
|
+
// Undo what started before the home is released, so no listener or child outlives it.
|
|
842
|
+
live.stop();
|
|
843
|
+
interaction.stop();
|
|
844
|
+
server.close();
|
|
845
|
+
await gateway.close();
|
|
846
|
+
await pool.stopAll();
|
|
847
|
+
await manager.stopAll();
|
|
848
|
+
bus.close();
|
|
849
|
+
stopRedactingEvents();
|
|
850
|
+
throw error;
|
|
851
|
+
}
|
|
852
|
+
// A daemon exits after 30 idle minutes unless it was started always-on (D62, RT-08); a
|
|
853
|
+
// host-spawned instance lives as long as its stdio.
|
|
854
|
+
if (options.mode === "daemon" && !options.alwaysOn) {
|
|
855
|
+
const testIdle = Number(environment["MOSTRA_TEST_IDLE_EXIT_MS"]);
|
|
856
|
+
idle = watchIdle({
|
|
857
|
+
...(testIdle > 0 ? { idleMs: testIdle } : {}),
|
|
858
|
+
busy: () => ({
|
|
859
|
+
clients: server.connections,
|
|
860
|
+
views: bus.activeViews(),
|
|
861
|
+
deliveries: interaction.deliveries().reduce((sum, owner) => sum + owner.pending, 0),
|
|
862
|
+
runs: actions.activity(),
|
|
863
|
+
producers: live.activity(),
|
|
864
|
+
subscriptions: interaction.subscriptionCount(),
|
|
865
|
+
devSessions: dev.count(),
|
|
866
|
+
// A home with a login service is always-on, whoever started its daemon (D133).
|
|
867
|
+
loginService: hasLoginService(home.path, environment) ? 1 : 0,
|
|
868
|
+
}),
|
|
869
|
+
onIdle: () => {
|
|
870
|
+
log.info("instance.idle_exit", {});
|
|
871
|
+
void instance.stop("idle", 0);
|
|
872
|
+
},
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
home.watch((diagnostic) => {
|
|
876
|
+
log.error("instance.home_gone", { message: diagnostic.message });
|
|
877
|
+
server.refuse(diagnostic);
|
|
878
|
+
void instance.stop("home gone", 3, diagnostic);
|
|
879
|
+
}, options.checkIntervalMs);
|
|
880
|
+
if (options.mode === "host" && options.stdio) {
|
|
881
|
+
server.serve("stdio", options.stdio.input, options.stdio.output, () => {
|
|
882
|
+
void instance.stop("the host closed stdio", 0);
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
log.info("instance.started", {
|
|
886
|
+
mode: options.mode,
|
|
887
|
+
pid: process.pid,
|
|
888
|
+
socket: home.socketPath,
|
|
889
|
+
shell: gateway.shellOrigin(),
|
|
890
|
+
});
|
|
891
|
+
return instance;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/** The longest test clock offset accepted: ten years. */
|
|
895
|
+
const MAX_CLOCK_OFFSET_MS = 10 * 365 * 24 * 60 * 60 * 1000;
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* `MOSTRA_TEST_CLOCK_OFFSET_MS`, the forward move of the gateway's clock that tests use to check
|
|
899
|
+
* link lifetimes. Only a whole number of milliseconds from 0 to ten years counts: a negative one
|
|
900
|
+
* would keep expiring links alive and a huge one would break every date. An offset in use is
|
|
901
|
+
* logged, so a forgotten one is visible.
|
|
902
|
+
*/
|
|
903
|
+
function testClockOffset(value: string | undefined, log: Log): number {
|
|
904
|
+
if (value === undefined || value === "") return 0;
|
|
905
|
+
const offset = Number(value);
|
|
906
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset > MAX_CLOCK_OFFSET_MS) {
|
|
907
|
+
log.warn("instance.test_clock_ignored", { value });
|
|
908
|
+
return 0;
|
|
909
|
+
}
|
|
910
|
+
if (offset > 0) log.warn("instance.test_clock", { offsetMs: offset });
|
|
911
|
+
return offset;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/**
|
|
915
|
+
* The browser-protocol revisions the gateway serves: every one Mostra shipped, plus, in tests, a
|
|
916
|
+
* newer test-only revision (`MOSTRA_TEST_BROWSER_REVISION_NEXT`), so a version frozen at today's
|
|
917
|
+
* revision is proven against a gateway at the next one (VR-10, D111). A value that isn't a dated
|
|
918
|
+
* revision newer than the newest is ignored and logged.
|
|
919
|
+
*/
|
|
920
|
+
function servedRevisions(next: string | undefined, log: Log): readonly string[] {
|
|
921
|
+
const shipped: readonly string[] = BROWSER_PROTOCOL_REVISIONS;
|
|
922
|
+
if (next === undefined || next === "") return shipped;
|
|
923
|
+
const newest = shipped.at(-1) ?? "";
|
|
924
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(next) || next <= newest) {
|
|
925
|
+
log.warn("instance.test_revision_ignored", { next });
|
|
926
|
+
return shipped;
|
|
927
|
+
}
|
|
928
|
+
log.warn("instance.test_revision", { next });
|
|
929
|
+
return [...shipped, next];
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/** How long a host's `secrets` port has to answer (FAILURE_MODEL §9). */
|
|
933
|
+
const PORT_TIMEOUT_MS = 30_000;
|
|
934
|
+
|
|
935
|
+
/** The directory of an artifact's source: the directory itself, or a single file's directory. */
|
|
936
|
+
function sourceDirectory(path: string): string | undefined {
|
|
937
|
+
try {
|
|
938
|
+
return statSync(path).isDirectory() ? path : dirname(path);
|
|
939
|
+
} catch {
|
|
940
|
+
return undefined;
|
|
941
|
+
}
|
|
942
|
+
}
|