@mostra/runtime 0.0.0-stage → 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 +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-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 -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 +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,28 @@
|
|
|
1
|
+
/** The ledger's driver-neutral query interface (D71: the store module hides the driver). */
|
|
2
|
+
|
|
3
|
+
/** A SQL parameter. */
|
|
4
|
+
export type SqlValue = string | number | bigint | null | Uint8Array;
|
|
5
|
+
|
|
6
|
+
/** What a write statement changed. */
|
|
7
|
+
export interface RunResult {
|
|
8
|
+
/** Rows inserted, updated or deleted. */
|
|
9
|
+
readonly changes: number;
|
|
10
|
+
/** The rowid of the last inserted row. */
|
|
11
|
+
readonly lastInsertRowid: number | bigint;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Queries inside one ledger transaction. Statements are prepared once and cached. */
|
|
15
|
+
export interface LedgerDb {
|
|
16
|
+
/** Runs a statement that returns no rows. */
|
|
17
|
+
run(sql: string, ...params: readonly SqlValue[]): RunResult;
|
|
18
|
+
/** Returns the first row, or `undefined`. */
|
|
19
|
+
get<T>(sql: string, ...params: readonly SqlValue[]): T | undefined;
|
|
20
|
+
/** Returns every row. */
|
|
21
|
+
all<T>(sql: string, ...params: readonly SqlValue[]): T[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** What a migration may do besides queries: run DDL scripts. */
|
|
25
|
+
export interface MigrationDb extends LedgerDb {
|
|
26
|
+
/** Runs one or more statements without parameters. */
|
|
27
|
+
exec(sql: string): void;
|
|
28
|
+
}
|
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live sources (D56, D63, LL-04, AC-07): values a page reads with `useLive(key)`, produced by the
|
|
3
|
+
* `live.ts` of the artifact's latest version in its worker, or pushed by an agent or host
|
|
4
|
+
* (`live/push`). The latest value persists with a revision that only grows, and in append mode a
|
|
5
|
+
* ring keeps the recent values; updates reach the views reading them as ephemeral messages, never
|
|
6
|
+
* as events, so a dashboard open all week never fills the event log.
|
|
7
|
+
*
|
|
8
|
+
* A producer runs while some view reads its source (a one-minute lease the page renews), while a
|
|
9
|
+
* host subscribes to the artifact's events, or always when its source says `always: true`.
|
|
10
|
+
*/
|
|
11
|
+
import {
|
|
12
|
+
type ArtifactRef,
|
|
13
|
+
type Batches,
|
|
14
|
+
createDiagnostic,
|
|
15
|
+
type Frame,
|
|
16
|
+
LIVE_RING_DEFAULT,
|
|
17
|
+
LIVE_RING_MAX,
|
|
18
|
+
LIVE_VALUE_MAX,
|
|
19
|
+
type LiveSourceInfo,
|
|
20
|
+
type LiveState,
|
|
21
|
+
livePushMethod,
|
|
22
|
+
RpcError,
|
|
23
|
+
ServerLiveValueParams,
|
|
24
|
+
serializeAll,
|
|
25
|
+
type WireError,
|
|
26
|
+
} from "@mostra/protocol";
|
|
27
|
+
import type { EphemeralBus, Interaction, PageIdentity } from "../interaction/index.ts";
|
|
28
|
+
import type { Ledger, LedgerDb, RequestKey } from "../ledger/index.ts";
|
|
29
|
+
import { recordedResult, recordResult, requestKey } from "../ledger/index.ts";
|
|
30
|
+
import { type Log, redactFrames, redactValue } from "../log.ts";
|
|
31
|
+
import { handler, type MethodHandler } from "../protocol-server/index.ts";
|
|
32
|
+
import type { WorkerCrash, WorkerHandle, WorkerPool, WorkerVersion } from "../workers/index.ts";
|
|
33
|
+
|
|
34
|
+
/** What the live module knows of an artifact's latest version. */
|
|
35
|
+
export interface LatestLive {
|
|
36
|
+
readonly versionId: string;
|
|
37
|
+
/**
|
|
38
|
+
* The sources its `live.ts` defines, from its registry: `always` is `true` or `false` when the
|
|
39
|
+
* builder read it as a literal, `undefined` when it couldn't (the worker then tells).
|
|
40
|
+
*/
|
|
41
|
+
readonly sources: readonly { readonly key: string; readonly always?: boolean }[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Options for {@link createLive}. */
|
|
45
|
+
export interface LiveOptions {
|
|
46
|
+
readonly ledger: Ledger;
|
|
47
|
+
readonly log: Log;
|
|
48
|
+
readonly bus: EphemeralBus;
|
|
49
|
+
readonly pool: WorkerPool;
|
|
50
|
+
readonly interaction: Pick<Interaction, "subscribedArtifacts" | "checkView">;
|
|
51
|
+
/** The latest version of an artifact with live sources, or `undefined`. */
|
|
52
|
+
readonly latest: (artifactId: string) => LatestLive | undefined;
|
|
53
|
+
/** Every artifact whose latest version defines live sources. */
|
|
54
|
+
readonly artifactsWithLive: () => readonly string[];
|
|
55
|
+
/** What the pool needs to start a version's worker. */
|
|
56
|
+
readonly version: (versionId: string) => WorkerVersion | undefined;
|
|
57
|
+
/** How often the producers are checked against who wants them; default 5 s. */
|
|
58
|
+
readonly reconcileMs?: number;
|
|
59
|
+
/** Removes secrets from every value stored (AC-06). */
|
|
60
|
+
readonly redactor?: { redact(text: string): string };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The live module. */
|
|
64
|
+
export interface Live {
|
|
65
|
+
/** Reads sources for a page and keeps them produced for its view for a minute (LL-04). */
|
|
66
|
+
read(
|
|
67
|
+
page: PageIdentity,
|
|
68
|
+
input: {
|
|
69
|
+
readonly viewId: string;
|
|
70
|
+
readonly keys: readonly {
|
|
71
|
+
readonly key: string;
|
|
72
|
+
readonly window?: number;
|
|
73
|
+
readonly refresh?: boolean;
|
|
74
|
+
}[];
|
|
75
|
+
},
|
|
76
|
+
): LiveState[];
|
|
77
|
+
/** `live/push` (D56): a value from an agent or a host. */
|
|
78
|
+
push(
|
|
79
|
+
artifactId: string,
|
|
80
|
+
input: { readonly key: string; readonly value: unknown; readonly mode?: "replace" | "append" },
|
|
81
|
+
requestKey?: RequestKey,
|
|
82
|
+
): Promise<{ revision: string }>;
|
|
83
|
+
/** A worker's `server/live.value`. */
|
|
84
|
+
produced(
|
|
85
|
+
worker: { readonly artifactId: string; readonly versionId: string },
|
|
86
|
+
params: unknown,
|
|
87
|
+
): void;
|
|
88
|
+
/** A worker crashed: its producers start again when its backoff ends. */
|
|
89
|
+
crashed(crash: WorkerCrash): void;
|
|
90
|
+
/** Checks the producers against who wants them now. */
|
|
91
|
+
reconcile(): void;
|
|
92
|
+
/**
|
|
93
|
+
* A publish cleared the artifact's park (FAILURE_MODEL §2): its producers may start again,
|
|
94
|
+
* also when the publish reused the parked version.
|
|
95
|
+
*/
|
|
96
|
+
unpark(artifactId: string): void;
|
|
97
|
+
/**
|
|
98
|
+
* A version's worker was replaced on purpose (a dev session's server code built again, D140
|
|
99
|
+
* item 12): its producers ran in the old worker, so they start again in the new one, from the
|
|
100
|
+
* sources it describes.
|
|
101
|
+
*/
|
|
102
|
+
replaced(versionId: string): void;
|
|
103
|
+
/** How many producers run: the daemon stays while any do (D62). */
|
|
104
|
+
activity(): number;
|
|
105
|
+
/** Starts checking producers. */
|
|
106
|
+
start(): void;
|
|
107
|
+
/** Stops checking; the pool stops the producers with their workers. */
|
|
108
|
+
stop(): void;
|
|
109
|
+
/** The artifact's sources whose producers failed last, with the error each reported (verify, D138). */
|
|
110
|
+
failures(artifactId: string): { readonly key: string; readonly error: WireError }[];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface LiveRow {
|
|
114
|
+
readonly revision: number;
|
|
115
|
+
readonly value_json: string;
|
|
116
|
+
readonly at: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const keyOf = (artifactId: string, key: string): string => `${artifactId}\u0000${key}`;
|
|
120
|
+
|
|
121
|
+
/** After a worker couldn't start to describe its sources, how long before it is tried again. */
|
|
122
|
+
const DISCOVERY_RETRY_MS = 60_000;
|
|
123
|
+
|
|
124
|
+
function tooLarge(key: string): RpcError {
|
|
125
|
+
return new RpcError(
|
|
126
|
+
createDiagnostic("MOSTRA_LIVE_TOO_LARGE", {
|
|
127
|
+
message: `The value for the live source ${key} is larger than 4 MiB once serialized; it was not stored.`,
|
|
128
|
+
}),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Creates the live module. */
|
|
133
|
+
export function createLive(options: LiveOptions): Live {
|
|
134
|
+
const { ledger, log, bus, pool } = options;
|
|
135
|
+
/** Producers running now, by artifact and key, with the version producing. */
|
|
136
|
+
const running = new Map<
|
|
137
|
+
string,
|
|
138
|
+
{ artifactId: string; key: string; versionId: string; handle: WorkerHandle }
|
|
139
|
+
>();
|
|
140
|
+
/** Producers being started. */
|
|
141
|
+
const starting = new Set<string>();
|
|
142
|
+
/** Each version's sources as its worker described them (`ring`, `always`). */
|
|
143
|
+
const described = new Map<string, readonly LiveSourceInfo[]>();
|
|
144
|
+
/** The last failure of each source, shown until its next good value. */
|
|
145
|
+
const failures = new Map<string, WireError>();
|
|
146
|
+
/** Versions whose producers wait for their worker's backoff. */
|
|
147
|
+
const retryAt = new Map<string, number>();
|
|
148
|
+
/** Versions whose producers stopped because their worker is parked, by version, to their artifact. */
|
|
149
|
+
const parkedVersions = new Map<string, string>();
|
|
150
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
151
|
+
let scheduled = false;
|
|
152
|
+
let stopped = false;
|
|
153
|
+
/** A worker is starting only to say which of its version's sources are `always`. */
|
|
154
|
+
let discovering = false;
|
|
155
|
+
|
|
156
|
+
const ringOf = (key: string, versionId: string | undefined): number | undefined => {
|
|
157
|
+
if (!versionId) return undefined;
|
|
158
|
+
return described.get(versionId)?.find((source) => source.key === key)?.ring;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** Stores a value under the next revision, and in the ring when the source keeps one. */
|
|
162
|
+
const store = (
|
|
163
|
+
db: LedgerDb,
|
|
164
|
+
artifactId: string,
|
|
165
|
+
key: string,
|
|
166
|
+
value: Batches,
|
|
167
|
+
producerVersionId: string | null,
|
|
168
|
+
ring: number | undefined,
|
|
169
|
+
): { revision: number; at: string } => {
|
|
170
|
+
const current = db.get<{ revision: number }>(
|
|
171
|
+
"SELECT revision FROM live_value WHERE artifact_id = ? AND key = ?",
|
|
172
|
+
artifactId,
|
|
173
|
+
key,
|
|
174
|
+
);
|
|
175
|
+
const revision = (current?.revision ?? 0) + 1;
|
|
176
|
+
const at = new Date().toISOString();
|
|
177
|
+
const json = JSON.stringify(redactBatches(value));
|
|
178
|
+
db.run(
|
|
179
|
+
`INSERT INTO live_value (artifact_id, key, revision, value_json, producer_version_id, at)
|
|
180
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
181
|
+
ON CONFLICT (artifact_id, key) DO UPDATE SET revision = excluded.revision,
|
|
182
|
+
value_json = excluded.value_json, producer_version_id = excluded.producer_version_id,
|
|
183
|
+
at = excluded.at`,
|
|
184
|
+
artifactId,
|
|
185
|
+
key,
|
|
186
|
+
revision,
|
|
187
|
+
json,
|
|
188
|
+
producerVersionId,
|
|
189
|
+
at,
|
|
190
|
+
);
|
|
191
|
+
if (ring !== undefined && ring > 0) {
|
|
192
|
+
db.run(
|
|
193
|
+
"INSERT INTO live_ring (artifact_id, key, revision, value_json, at) VALUES (?, ?, ?, ?, ?)",
|
|
194
|
+
artifactId,
|
|
195
|
+
key,
|
|
196
|
+
revision,
|
|
197
|
+
json,
|
|
198
|
+
at,
|
|
199
|
+
);
|
|
200
|
+
db.run(
|
|
201
|
+
"DELETE FROM live_ring WHERE artifact_id = ? AND key = ? AND revision <= ?",
|
|
202
|
+
artifactId,
|
|
203
|
+
key,
|
|
204
|
+
revision - Math.min(ring, LIVE_RING_MAX),
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
return { revision, at };
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const publishValue = (
|
|
211
|
+
artifactId: string,
|
|
212
|
+
key: string,
|
|
213
|
+
revision: number,
|
|
214
|
+
at: string,
|
|
215
|
+
value: Batches,
|
|
216
|
+
): void => {
|
|
217
|
+
bus.publishLive(artifactId, {
|
|
218
|
+
kind: "live",
|
|
219
|
+
live: {
|
|
220
|
+
key,
|
|
221
|
+
revision: String(revision),
|
|
222
|
+
at,
|
|
223
|
+
value: redactBatches(value),
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/** A live value's batches as frames, redacted as values (D131; Opus review 7). */
|
|
229
|
+
const redactBatches = (value: Batches): Batches => {
|
|
230
|
+
const redactor = options.redactor;
|
|
231
|
+
return redactor ? value.map((batch) => redactFrames([...batch], redactor)) : value;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const fail = (artifactId: string, key: string, raw: WireError): void => {
|
|
235
|
+
// An error is output too: a producer's message may carry a secret it read (AC-06).
|
|
236
|
+
const error = options.redactor ? redactValue(raw, options.redactor) : raw;
|
|
237
|
+
failures.set(keyOf(artifactId, key), error);
|
|
238
|
+
bus.publishLive(artifactId, { kind: "live", live: { key, error } });
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const wanted = (): Map<string, { artifactId: string; key: string }> => {
|
|
242
|
+
const out = new Map<string, { artifactId: string; key: string }>();
|
|
243
|
+
const add = (artifactId: string, key: string): void => {
|
|
244
|
+
out.set(keyOf(artifactId, key), { artifactId, key });
|
|
245
|
+
};
|
|
246
|
+
for (const entry of bus.liveInterest()) add(entry.artifactId, entry.key);
|
|
247
|
+
const subscribed = options.interaction.subscribedArtifacts();
|
|
248
|
+
for (const artifactId of options.artifactsWithLive()) {
|
|
249
|
+
const latest = options.latest(artifactId);
|
|
250
|
+
if (!latest) continue;
|
|
251
|
+
const known = described.get(latest.versionId);
|
|
252
|
+
for (const source of latest.sources) {
|
|
253
|
+
const always = known?.find((info) => info.key === source.key)?.always ?? source.always;
|
|
254
|
+
if (subscribed.has(artifactId) || always === true) add(artifactId, source.key);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return out;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const startProducer = async (
|
|
261
|
+
artifactId: string,
|
|
262
|
+
key: string,
|
|
263
|
+
versionId: string,
|
|
264
|
+
): Promise<void> => {
|
|
265
|
+
const id = keyOf(artifactId, key);
|
|
266
|
+
const version = options.version(versionId);
|
|
267
|
+
if (!version) return;
|
|
268
|
+
starting.add(id);
|
|
269
|
+
try {
|
|
270
|
+
const handle = await pool.acquire(version);
|
|
271
|
+
described.set(versionId, handle.started.live);
|
|
272
|
+
if (!handle.started.live.some((source) => source.key === key)) return;
|
|
273
|
+
// Known before it starts: its first value can arrive before the start's answer.
|
|
274
|
+
running.set(id, { artifactId, key, versionId, handle });
|
|
275
|
+
try {
|
|
276
|
+
await handle.liveStart(key);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
running.delete(id);
|
|
279
|
+
throw error;
|
|
280
|
+
}
|
|
281
|
+
} catch (error) {
|
|
282
|
+
const diagnostic = error instanceof RpcError ? error.diagnostic : undefined;
|
|
283
|
+
fail(artifactId, key, {
|
|
284
|
+
message: diagnostic?.message ?? String(error),
|
|
285
|
+
code: diagnostic?.code ?? "MOSTRA_LIVE_SOURCE_FAILED",
|
|
286
|
+
});
|
|
287
|
+
} finally {
|
|
288
|
+
starting.delete(id);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const reconcileNow = (): void => {
|
|
293
|
+
if (stopped) return;
|
|
294
|
+
const want = wanted();
|
|
295
|
+
const now = Date.now();
|
|
296
|
+
// Versions whose `always` the registry couldn't read: their worker says. One at a time, and
|
|
297
|
+
// a worker started only to say stops at once unless it has a producer to run, so a home of
|
|
298
|
+
// many such artifacts never starts many workers together (Opus review 2).
|
|
299
|
+
if (!discovering) {
|
|
300
|
+
for (const artifactId of options.artifactsWithLive()) {
|
|
301
|
+
const latest = options.latest(artifactId);
|
|
302
|
+
if (!latest || described.has(latest.versionId)) continue;
|
|
303
|
+
if (!latest.sources.some((source) => source.always === undefined)) continue;
|
|
304
|
+
if ((retryAt.get(latest.versionId) ?? 0) > now) continue;
|
|
305
|
+
const version = options.version(latest.versionId);
|
|
306
|
+
if (!version) continue;
|
|
307
|
+
discovering = true;
|
|
308
|
+
void pool
|
|
309
|
+
.acquire(version)
|
|
310
|
+
.then(
|
|
311
|
+
(handle) => {
|
|
312
|
+
described.set(latest.versionId, handle.started.live);
|
|
313
|
+
const needed =
|
|
314
|
+
handle.started.live.some((source) => source.always === true) ||
|
|
315
|
+
[...want.values()].some((entry) => entry.artifactId === artifactId);
|
|
316
|
+
if (!needed) pool.release(latest.versionId);
|
|
317
|
+
},
|
|
318
|
+
() => {
|
|
319
|
+
// Never sooner than a crash's backoff or park asked for.
|
|
320
|
+
const later = Date.now() + DISCOVERY_RETRY_MS;
|
|
321
|
+
retryAt.set(latest.versionId, Math.max(retryAt.get(latest.versionId) ?? 0, later));
|
|
322
|
+
},
|
|
323
|
+
)
|
|
324
|
+
.finally(() => {
|
|
325
|
+
discovering = false;
|
|
326
|
+
schedule();
|
|
327
|
+
});
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
for (const [id, producer] of running) {
|
|
332
|
+
const latest = options.latest(producer.artifactId);
|
|
333
|
+
if (!want.has(id) || latest?.versionId !== producer.versionId) {
|
|
334
|
+
running.delete(id);
|
|
335
|
+
void producer.handle.liveStop(producer.key);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
for (const [id, entry] of want) {
|
|
339
|
+
if (running.has(id) || starting.has(id)) continue;
|
|
340
|
+
const latest = options.latest(entry.artifactId);
|
|
341
|
+
if (!latest?.sources.some((source) => source.key === entry.key)) continue;
|
|
342
|
+
if ((retryAt.get(latest.versionId) ?? 0) > now) continue;
|
|
343
|
+
void startProducer(entry.artifactId, entry.key, latest.versionId);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const schedule = (): void => {
|
|
348
|
+
if (scheduled || stopped) return;
|
|
349
|
+
scheduled = true;
|
|
350
|
+
setImmediate(() => {
|
|
351
|
+
scheduled = false;
|
|
352
|
+
try {
|
|
353
|
+
reconcileNow();
|
|
354
|
+
} catch (error) {
|
|
355
|
+
log.error("live.reconcile_failed", { error });
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const stateOf = (db: LedgerDb, artifactId: string, key: string, window?: number): LiveState => {
|
|
361
|
+
const row = db.get<LiveRow>(
|
|
362
|
+
"SELECT revision, value_json, at FROM live_value WHERE artifact_id = ? AND key = ?",
|
|
363
|
+
artifactId,
|
|
364
|
+
key,
|
|
365
|
+
);
|
|
366
|
+
const failure = failures.get(keyOf(artifactId, key));
|
|
367
|
+
const ring =
|
|
368
|
+
window === undefined
|
|
369
|
+
? undefined
|
|
370
|
+
: db
|
|
371
|
+
.all<LiveRow>(
|
|
372
|
+
"SELECT revision, value_json, at FROM live_ring WHERE artifact_id = ? AND key = ? ORDER BY revision DESC LIMIT ?",
|
|
373
|
+
artifactId,
|
|
374
|
+
key,
|
|
375
|
+
window,
|
|
376
|
+
)
|
|
377
|
+
.reverse()
|
|
378
|
+
.map((entry) => ({
|
|
379
|
+
revision: String(entry.revision),
|
|
380
|
+
at: entry.at,
|
|
381
|
+
value: JSON.parse(entry.value_json) as Batches,
|
|
382
|
+
}));
|
|
383
|
+
return {
|
|
384
|
+
key,
|
|
385
|
+
...(row
|
|
386
|
+
? {
|
|
387
|
+
revision: String(row.revision),
|
|
388
|
+
at: row.at,
|
|
389
|
+
value: JSON.parse(row.value_json) as Batches,
|
|
390
|
+
}
|
|
391
|
+
: {}),
|
|
392
|
+
...(ring ? { ring } : {}),
|
|
393
|
+
...(failure ? { error: failure } : {}),
|
|
394
|
+
};
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
return {
|
|
398
|
+
failures(artifactId) {
|
|
399
|
+
const prefix = keyOf(artifactId, "");
|
|
400
|
+
return [...failures]
|
|
401
|
+
.filter(([id]) => id.startsWith(prefix))
|
|
402
|
+
.map(([id, error]) => ({ key: id.slice(prefix.length), error }));
|
|
403
|
+
},
|
|
404
|
+
read(page, input) {
|
|
405
|
+
options.interaction.checkView(page, input.viewId);
|
|
406
|
+
bus.watchLive(page.artifactId, input.viewId, input.keys);
|
|
407
|
+
for (const entry of input.keys) {
|
|
408
|
+
if (entry.refresh)
|
|
409
|
+
running.get(keyOf(page.artifactId, entry.key))?.handle.liveRefresh(entry.key);
|
|
410
|
+
}
|
|
411
|
+
schedule();
|
|
412
|
+
return ledger.read((db) =>
|
|
413
|
+
input.keys.map((entry) => stateOf(db, page.artifactId, entry.key, entry.window)),
|
|
414
|
+
);
|
|
415
|
+
},
|
|
416
|
+
|
|
417
|
+
async push(artifactId, input, requestKey) {
|
|
418
|
+
let batches: Batches;
|
|
419
|
+
try {
|
|
420
|
+
batches = await serializeAll(input.value, { mode: "argument" });
|
|
421
|
+
} catch {
|
|
422
|
+
throw new RpcError(
|
|
423
|
+
createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", {
|
|
424
|
+
message: `The value pushed to ${input.key} isn't JSON.`,
|
|
425
|
+
}),
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
if (JSON.stringify(batches).length > LIVE_VALUE_MAX) throw tooLarge(input.key);
|
|
429
|
+
const latest = options.latest(artifactId);
|
|
430
|
+
const ring =
|
|
431
|
+
input.mode === "append"
|
|
432
|
+
? (ringOf(input.key, latest?.versionId) ?? LIVE_RING_DEFAULT)
|
|
433
|
+
: ringOf(input.key, latest?.versionId);
|
|
434
|
+
const result = ledger.write((db) => {
|
|
435
|
+
if (requestKey) {
|
|
436
|
+
const replay = recordedResult<{ revision: string }>(db, requestKey);
|
|
437
|
+
if (replay) return { replay };
|
|
438
|
+
}
|
|
439
|
+
const stored = store(db, artifactId, input.key, batches, null, ring);
|
|
440
|
+
const answer = { revision: String(stored.revision) };
|
|
441
|
+
if (requestKey) recordResult(db, requestKey, answer);
|
|
442
|
+
return { stored, answer };
|
|
443
|
+
});
|
|
444
|
+
if ("replay" in result && result.replay) return result.replay;
|
|
445
|
+
const { stored, answer } = result as {
|
|
446
|
+
stored: { revision: number; at: string };
|
|
447
|
+
answer: { revision: string };
|
|
448
|
+
};
|
|
449
|
+
failures.delete(keyOf(artifactId, input.key));
|
|
450
|
+
publishValue(artifactId, input.key, stored.revision, stored.at, batches);
|
|
451
|
+
return answer;
|
|
452
|
+
},
|
|
453
|
+
|
|
454
|
+
produced(worker, params) {
|
|
455
|
+
const parsed = ServerLiveValueParams.safeParse(params);
|
|
456
|
+
if (!parsed.success) return;
|
|
457
|
+
const { key } = parsed.data;
|
|
458
|
+
const producer = running.get(keyOf(worker.artifactId, key));
|
|
459
|
+
// Only the producer of the latest version writes the source; a stale one is ignored.
|
|
460
|
+
if (!producer || producer.versionId !== worker.versionId) return;
|
|
461
|
+
if (parsed.data.error) {
|
|
462
|
+
fail(worker.artifactId, key, parsed.data.error);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const value = parsed.data.value as Batches | undefined;
|
|
466
|
+
if (!value) return;
|
|
467
|
+
if (JSON.stringify(value).length > LIVE_VALUE_MAX) {
|
|
468
|
+
fail(worker.artifactId, key, {
|
|
469
|
+
code: "MOSTRA_LIVE_TOO_LARGE",
|
|
470
|
+
message: tooLarge(key).diagnostic.message,
|
|
471
|
+
});
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const ring = ringOf(key, worker.versionId);
|
|
475
|
+
try {
|
|
476
|
+
const stored = ledger.write((db) =>
|
|
477
|
+
store(db, worker.artifactId, key, value as Frame[][], worker.versionId, ring),
|
|
478
|
+
);
|
|
479
|
+
failures.delete(keyOf(worker.artifactId, key));
|
|
480
|
+
publishValue(worker.artifactId, key, stored.revision, stored.at, value);
|
|
481
|
+
} catch (error) {
|
|
482
|
+
log.error("live.store_failed", { key, error });
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
|
|
486
|
+
crashed(crash) {
|
|
487
|
+
for (const [id, producer] of running) {
|
|
488
|
+
if (producer.versionId === crash.versionId) running.delete(id);
|
|
489
|
+
}
|
|
490
|
+
if (crash.parked) {
|
|
491
|
+
for (const key of crash.liveKeys) {
|
|
492
|
+
fail(crash.artifactId, key, {
|
|
493
|
+
code: "MOSTRA_RUNTIME_WORKER_PARKED",
|
|
494
|
+
message: `The artifact's server code keeps crashing, so its live sources stopped (${crash.cause}).`,
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
retryAt.set(crash.versionId, Number.POSITIVE_INFINITY);
|
|
498
|
+
parkedVersions.set(crash.versionId, crash.artifactId);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
retryAt.set(crash.versionId, Date.now() + crash.backoffMs);
|
|
502
|
+
setTimeout(() => {
|
|
503
|
+
retryAt.delete(crash.versionId);
|
|
504
|
+
schedule();
|
|
505
|
+
}, crash.backoffMs + 10).unref?.();
|
|
506
|
+
},
|
|
507
|
+
|
|
508
|
+
reconcile: schedule,
|
|
509
|
+
|
|
510
|
+
replaced(versionId) {
|
|
511
|
+
for (const [id, producer] of running) {
|
|
512
|
+
if (producer.versionId === versionId) running.delete(id);
|
|
513
|
+
}
|
|
514
|
+
described.delete(versionId);
|
|
515
|
+
retryAt.delete(versionId);
|
|
516
|
+
schedule();
|
|
517
|
+
},
|
|
518
|
+
|
|
519
|
+
unpark(artifactId) {
|
|
520
|
+
let cleared = false;
|
|
521
|
+
for (const [versionId, owner] of parkedVersions) {
|
|
522
|
+
if (owner !== artifactId) continue;
|
|
523
|
+
parkedVersions.delete(versionId);
|
|
524
|
+
retryAt.delete(versionId);
|
|
525
|
+
cleared = true;
|
|
526
|
+
}
|
|
527
|
+
if (!cleared) return;
|
|
528
|
+
for (const [id, error] of failures) {
|
|
529
|
+
if (id.startsWith(keyOf(artifactId, "")) && error.code === "MOSTRA_RUNTIME_WORKER_PARKED") {
|
|
530
|
+
failures.delete(id);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
schedule();
|
|
534
|
+
},
|
|
535
|
+
|
|
536
|
+
activity() {
|
|
537
|
+
return running.size + starting.size;
|
|
538
|
+
},
|
|
539
|
+
|
|
540
|
+
start() {
|
|
541
|
+
stopped = false;
|
|
542
|
+
bus.onLiveInterest(schedule);
|
|
543
|
+
timer = setInterval(schedule, options.reconcileMs ?? 5_000);
|
|
544
|
+
timer.unref?.();
|
|
545
|
+
schedule();
|
|
546
|
+
},
|
|
547
|
+
|
|
548
|
+
stop() {
|
|
549
|
+
stopped = true;
|
|
550
|
+
if (timer !== undefined) clearInterval(timer);
|
|
551
|
+
},
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** `live/push` (D56): a value from an agent or host into an artifact's live source. */
|
|
556
|
+
export function liveMethods(
|
|
557
|
+
live: Live,
|
|
558
|
+
resolveArtifact: (ref: ArtifactRef) => string,
|
|
559
|
+
): MethodHandler[] {
|
|
560
|
+
return [
|
|
561
|
+
handler(livePushMethod, (params) =>
|
|
562
|
+
live.push(
|
|
563
|
+
resolveArtifact(params.artifact),
|
|
564
|
+
{
|
|
565
|
+
key: params.key,
|
|
566
|
+
value: params.value,
|
|
567
|
+
...(params.mode !== undefined ? { mode: params.mode } : {}),
|
|
568
|
+
},
|
|
569
|
+
requestKey(livePushMethod.method, params),
|
|
570
|
+
),
|
|
571
|
+
),
|
|
572
|
+
];
|
|
573
|
+
}
|