@mostra/runtime 0.9.0 → 0.10.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/dist/bin.js +2879 -8
- package/dist/bin.js.map +1 -1
- package/dist/builder/child/main.js +2 -2
- package/dist/{child-Chiavhdx.js → child-BmNZ4a70.js} +2 -2
- package/dist/{child-Chiavhdx.js.map → child-BmNZ4a70.js.map} +1 -1
- package/dist/deps/child/install.js +1 -1
- package/dist/{directories-mJ1qlVht.js → directories-BLJjeSPD.js} +5 -5
- package/dist/directories-BLJjeSPD.js.map +1 -0
- package/dist/{files-FygSJIzK.js → files-DGeWuE4I.js} +2 -2
- package/dist/{files-FygSJIzK.js.map → files-DGeWuE4I.js.map} +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/{instance-EnaZPs14.js → instance-FExcyENK.js} +3341 -396
- package/dist/instance-FExcyENK.js.map +1 -0
- package/package.json +7 -5
- package/src/actions/index.ts +163 -10
- package/src/bin.ts +78 -1
- package/src/gateway/actions.ts +20 -4
- package/src/gateway/browser.ts +118 -95
- package/src/gateway/capabilities.ts +15 -6
- package/src/gateway/index.ts +112 -15
- package/src/gateway/routes.ts +15 -7
- package/src/gc/index.ts +14 -0
- package/src/instance/index.ts +41 -2
- package/src/interaction/ephemeral.ts +32 -23
- package/src/interaction/events.ts +21 -1
- package/src/interaction/imports.ts +317 -0
- package/src/interaction/index.ts +172 -19
- package/src/interaction/outbox.ts +9 -0
- package/src/interaction/results.ts +8 -1
- package/src/interaction/runs.ts +52 -2
- package/src/interaction/submissions.ts +17 -1
- package/src/interaction/tally.ts +129 -0
- package/src/ledger/index.ts +104 -9
- package/src/ledger/migrations.ts +183 -0
- package/src/ledger/types.ts +8 -0
- package/src/link/binding.ts +289 -0
- package/src/link/hosted.ts +65 -0
- package/src/link/http.ts +136 -0
- package/src/link/index.ts +8 -0
- package/src/link/manager.ts +1385 -0
- package/src/link/relay.ts +512 -0
- package/src/live/index.ts +112 -2
- package/src/platform/filesystems.ts +78 -0
- package/src/platform/index.ts +1 -0
- package/src/protocol-server/index.ts +18 -1
- package/src/protocol-server/server.ts +38 -7
- package/src/protocol-server/websocket.ts +161 -0
- package/src/publish/bundle.ts +398 -0
- package/src/publish/manifest.ts +9 -0
- package/src/publish/methods.ts +12 -1
- package/src/publish/publisher.ts +127 -0
- package/src/registry/index.ts +35 -14
- package/src/server/api.ts +439 -0
- package/src/server/auth.ts +613 -0
- package/src/server/config.ts +221 -0
- package/src/server/data.ts +369 -0
- package/src/server/directory.ts +141 -0
- package/src/server/hosted.ts +822 -0
- package/src/server/index.ts +368 -0
- package/src/server/relay.ts +1163 -0
- package/src/share/index.ts +110 -6
- package/dist/directories-mJ1qlVht.js.map +0 -1
- package/dist/instance-EnaZPs14.js.map +0 -1
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The relay on the owner's side (D165 item 4; INTERFACES §18.3): the owner instance runs a hosted
|
|
3
|
+
* artifact's runs and plain calls that its hosted home relays, in its own pool with its own
|
|
4
|
+
* secrets. A relayed run is recorded here under the hosted run's ID before the answer says it was
|
|
5
|
+
* accepted, so a repeated `port/relay.run` joins it and starts nothing; its progress, log, result
|
|
6
|
+
* and end go back as `relay/run.update`, already redacted by the actions module. What a dropped
|
|
7
|
+
* connection loses, the hosted home's reconcile after the reconnect reads again from this ledger.
|
|
8
|
+
* No relay message carries a secret: `secret()` is answered here (D129).
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
createDiagnostic,
|
|
12
|
+
type PortRelayCallParams,
|
|
13
|
+
type PortRelayReconcileParams,
|
|
14
|
+
type PortRelayReconcileResult,
|
|
15
|
+
type PortRelayRunParams,
|
|
16
|
+
portRelayCallMethod,
|
|
17
|
+
portRelayCancelMethod,
|
|
18
|
+
portRelayLiveMethod,
|
|
19
|
+
portRelayReconcileMethod,
|
|
20
|
+
portRelayRunMethod,
|
|
21
|
+
type RelayRunUpdateParams,
|
|
22
|
+
RpcError,
|
|
23
|
+
relayCallBatchMethod,
|
|
24
|
+
relayEffectMethod,
|
|
25
|
+
relayLiveValueMethod,
|
|
26
|
+
relayRunUpdateMethod,
|
|
27
|
+
type ServerLiveValueParams,
|
|
28
|
+
} from "@mostra/protocol";
|
|
29
|
+
import type { Actions, RelayedEffect, RunObserver } from "../actions/index.ts";
|
|
30
|
+
import { appendEvent, type RunRow, runState } from "../interaction/index.ts";
|
|
31
|
+
import type { Ledger } from "../ledger/index.ts";
|
|
32
|
+
import type { Log } from "../log.ts";
|
|
33
|
+
import type { BindingSession } from "./binding.ts";
|
|
34
|
+
|
|
35
|
+
/** What the owner's relay needs. */
|
|
36
|
+
export interface OwnerRelayOptions {
|
|
37
|
+
readonly ledger: Ledger;
|
|
38
|
+
readonly log: Log;
|
|
39
|
+
readonly actions: Pick<Actions, "runRelayed" | "cancelRelayed" | "callRelayed">;
|
|
40
|
+
readonly now: () => number;
|
|
41
|
+
/**
|
|
42
|
+
* Tests: this many accepted runs lose their acknowledgement (the binding drops before the
|
|
43
|
+
* answer), so the hosted home sends them again (`MOSTRA_TEST_RELAY_LOSE_ACK`).
|
|
44
|
+
*/
|
|
45
|
+
readonly loseAcks?: number;
|
|
46
|
+
/** The live sources hosted pages want changed: producers start or stop. */
|
|
47
|
+
readonly liveChanged?: () => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** How long a hosted page's interest in a live source lasts unless renewed (as a view's, D130). */
|
|
51
|
+
const LIVE_INTEREST_MS = 60_000;
|
|
52
|
+
|
|
53
|
+
/** The owner's relay, for one link manager. */
|
|
54
|
+
export interface OwnerRelay {
|
|
55
|
+
/** Whether `method` is one of the relay's port calls. */
|
|
56
|
+
handles(method: string): boolean;
|
|
57
|
+
/** Answers a relay port call on the binding of `server`. */
|
|
58
|
+
request(
|
|
59
|
+
server: string,
|
|
60
|
+
session: () => BindingSession | undefined | Promise<BindingSession | undefined>,
|
|
61
|
+
method: string,
|
|
62
|
+
params: unknown,
|
|
63
|
+
/** Drops the binding (a test's lost acknowledgement). */
|
|
64
|
+
drop: () => void,
|
|
65
|
+
): Promise<unknown>;
|
|
66
|
+
/** The binding of `server` closed: its relayed calls stop. */
|
|
67
|
+
closed(server: string): void;
|
|
68
|
+
/** The live sources hosted pages want now. */
|
|
69
|
+
liveInterest(): Iterable<{ readonly artifactId: string; readonly key: string }>;
|
|
70
|
+
/** Sends a hosted artifact's produced value to its hosted home on `server`. */
|
|
71
|
+
sendLive(
|
|
72
|
+
server: string,
|
|
73
|
+
session: () => BindingSession | undefined | Promise<BindingSession | undefined>,
|
|
74
|
+
artifactId: string,
|
|
75
|
+
versionId: string,
|
|
76
|
+
produced: ServerLiveValueParams,
|
|
77
|
+
): void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function invalid(message: string): RpcError {
|
|
81
|
+
return new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message }), -32602);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const RELAY_METHODS = new Set([
|
|
85
|
+
portRelayLiveMethod.method,
|
|
86
|
+
portRelayRunMethod.method,
|
|
87
|
+
portRelayCancelMethod.method,
|
|
88
|
+
portRelayReconcileMethod.method,
|
|
89
|
+
portRelayCallMethod.method,
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
/** Creates the owner's relay. */
|
|
93
|
+
export function createOwnerRelay(options: OwnerRelayOptions): OwnerRelay {
|
|
94
|
+
const { ledger, log } = options;
|
|
95
|
+
let loseAcks = options.loseAcks ?? 0;
|
|
96
|
+
/** Hosted pages' interest in live sources: `<server> <artifact> <key>` → until when. */
|
|
97
|
+
const interest = new Map<string, { artifactId: string; key: string; until: number }>();
|
|
98
|
+
const interestSweep = setInterval(() => {
|
|
99
|
+
let changed = false;
|
|
100
|
+
for (const [id, entry] of interest) {
|
|
101
|
+
if (entry.until <= options.now()) {
|
|
102
|
+
interest.delete(id);
|
|
103
|
+
changed = true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (changed) options.liveChanged?.();
|
|
107
|
+
}, 5_000);
|
|
108
|
+
interestSweep.unref();
|
|
109
|
+
/** Relayed calls in flight per server, so a lost binding stops them. */
|
|
110
|
+
const calls = new Map<string, Map<string, AbortController>>();
|
|
111
|
+
/** Each relayed run's last update sequence, from `relayed_run.seq_sent`. */
|
|
112
|
+
const sequences = new Map<string, number>();
|
|
113
|
+
/** Each run's updates in order: a run's messages never overtake each other. */
|
|
114
|
+
const chains = new Map<string, Promise<void>>();
|
|
115
|
+
|
|
116
|
+
const nextSeq = (runId: string): number => {
|
|
117
|
+
const known =
|
|
118
|
+
sequences.get(runId) ??
|
|
119
|
+
ledger.read(
|
|
120
|
+
(db) =>
|
|
121
|
+
db.get<{ seq_sent: number }>("SELECT seq_sent FROM relayed_run WHERE run_id = ?", runId)
|
|
122
|
+
?.seq_sent ?? 0,
|
|
123
|
+
);
|
|
124
|
+
const seq = known + 1;
|
|
125
|
+
sequences.set(runId, seq);
|
|
126
|
+
ledger.write((db) =>
|
|
127
|
+
db.run("UPDATE relayed_run SET seq_sent = ? WHERE run_id = ?", seq, runId),
|
|
128
|
+
);
|
|
129
|
+
return seq;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/** A relayed run's or call's signals and host events, appended on its hosted home. */
|
|
133
|
+
const effectOf =
|
|
134
|
+
(
|
|
135
|
+
session: () => BindingSession | undefined | Promise<BindingSession | undefined>,
|
|
136
|
+
artifactId: string,
|
|
137
|
+
of: { readonly runId: string } | { readonly callId: string },
|
|
138
|
+
): RelayedEffect =>
|
|
139
|
+
async (kind, name, payload) => {
|
|
140
|
+
const open = await session();
|
|
141
|
+
if (!open) {
|
|
142
|
+
throw new RpcError(
|
|
143
|
+
createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", {
|
|
144
|
+
message: "The page's server can't be reached now, so the signal wasn't raised there.",
|
|
145
|
+
}),
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
const answer = await open.call(relayEffectMethod, {
|
|
149
|
+
artifactId,
|
|
150
|
+
...of,
|
|
151
|
+
kind,
|
|
152
|
+
name,
|
|
153
|
+
...(payload !== undefined ? { payload } : {}),
|
|
154
|
+
});
|
|
155
|
+
return answer.cursor;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/** Sends one update of a run, after the ones before it; one the binding loses is reconciled. */
|
|
159
|
+
const update = (
|
|
160
|
+
runId: string,
|
|
161
|
+
session: () => BindingSession | undefined | Promise<BindingSession | undefined>,
|
|
162
|
+
body: Omit<RelayRunUpdateParams, "runId" | "seq">,
|
|
163
|
+
): void => {
|
|
164
|
+
const previous = chains.get(runId) ?? Promise.resolve();
|
|
165
|
+
const next = previous.then(async () => {
|
|
166
|
+
const open = await session();
|
|
167
|
+
if (!open) return;
|
|
168
|
+
try {
|
|
169
|
+
await open.call(relayRunUpdateMethod, { runId, seq: nextSeq(runId), ...body });
|
|
170
|
+
} catch (error) {
|
|
171
|
+
log.warn("relay.update_failed", { runId, error });
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
chains.set(runId, next);
|
|
175
|
+
void next.then(() => {
|
|
176
|
+
if (chains.get(runId) === next) chains.delete(runId);
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const observer = (
|
|
181
|
+
runId: string,
|
|
182
|
+
session: () => BindingSession | undefined | Promise<BindingSession | undefined>,
|
|
183
|
+
): RunObserver => ({
|
|
184
|
+
running: () => update(runId, session, { running: true }),
|
|
185
|
+
output: (flush) =>
|
|
186
|
+
update(runId, session, {
|
|
187
|
+
log: flush.log,
|
|
188
|
+
logFrom: flush.logFrom,
|
|
189
|
+
batches: flush.batches,
|
|
190
|
+
batchFrom: flush.batchFrom,
|
|
191
|
+
...(flush.progress ? { progress: flush.progress } : {}),
|
|
192
|
+
}),
|
|
193
|
+
ended: (state) => {
|
|
194
|
+
ledger.write((db) =>
|
|
195
|
+
db.run(
|
|
196
|
+
"UPDATE relayed_run SET finished_at = ? WHERE run_id = ?",
|
|
197
|
+
new Date(options.now()).toISOString(),
|
|
198
|
+
runId,
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
if (
|
|
202
|
+
state.status === "succeeded" ||
|
|
203
|
+
state.status === "failed" ||
|
|
204
|
+
state.status === "cancelled"
|
|
205
|
+
) {
|
|
206
|
+
// The end carries the run's whole output from this ledger, so a hosted home that lost an
|
|
207
|
+
// update on the way (a dropped connection, a timed-out call) never ends a run without its
|
|
208
|
+
// result; it keeps only what it lacks (Opus review of WP-29, 4).
|
|
209
|
+
update(
|
|
210
|
+
runId,
|
|
211
|
+
session,
|
|
212
|
+
fullState(runId) ?? {
|
|
213
|
+
end: {
|
|
214
|
+
status: state.status,
|
|
215
|
+
...(state.reason !== undefined ? { reason: state.reason } : {}),
|
|
216
|
+
...(state.error ? { error: state.error } : {}),
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
/** A run's whole state from this ledger, as one update (its end too, once it ended). */
|
|
225
|
+
const fullState = (runId: string): Omit<RelayRunUpdateParams, "runId" | "seq"> | undefined => {
|
|
226
|
+
const state = ledger.read((db) => {
|
|
227
|
+
const row = db.get<RunRow>("SELECT * FROM action_run WHERE id = ?", runId);
|
|
228
|
+
return row ? runState(db, row) : undefined;
|
|
229
|
+
});
|
|
230
|
+
if (!state) return undefined;
|
|
231
|
+
const ended =
|
|
232
|
+
state.status === "succeeded" || state.status === "failed" || state.status === "cancelled";
|
|
233
|
+
return {
|
|
234
|
+
running: state.status !== "queued",
|
|
235
|
+
log: state.log,
|
|
236
|
+
logFrom: 0,
|
|
237
|
+
batches: state.batches,
|
|
238
|
+
batchFrom: 0,
|
|
239
|
+
...(state.progress ? { progress: state.progress } : {}),
|
|
240
|
+
...(ended
|
|
241
|
+
? {
|
|
242
|
+
end: {
|
|
243
|
+
status: state.status as "succeeded" | "failed" | "cancelled",
|
|
244
|
+
...(state.reason !== undefined ? { reason: state.reason } : {}),
|
|
245
|
+
...(state.error ? { error: state.error } : {}),
|
|
246
|
+
},
|
|
247
|
+
}
|
|
248
|
+
: {}),
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/** Records a relayed run under the hosted run's ID, once; then runs it. */
|
|
253
|
+
const run = (
|
|
254
|
+
server: string,
|
|
255
|
+
session: () => BindingSession | undefined | Promise<BindingSession | undefined>,
|
|
256
|
+
params: PortRelayRunParams,
|
|
257
|
+
drop: () => void,
|
|
258
|
+
): { accepted: true } => {
|
|
259
|
+
const repeated = ledger.write((db): boolean => {
|
|
260
|
+
const existing = db.get<{ id: string; artifact_id: string }>(
|
|
261
|
+
"SELECT id, artifact_id FROM action_run WHERE id = ?",
|
|
262
|
+
params.runId,
|
|
263
|
+
);
|
|
264
|
+
if (existing) {
|
|
265
|
+
if (existing.artifact_id !== params.artifactId)
|
|
266
|
+
throw invalid("That run is another artifact's.");
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
// Only an artifact this instance shares to that server is its to run (D165 item 1b).
|
|
270
|
+
const owned = db.get<{ artifact_id: string }>(
|
|
271
|
+
"SELECT artifact_id FROM hosted_artifact WHERE artifact_id = ? AND server = ?",
|
|
272
|
+
params.artifactId,
|
|
273
|
+
server,
|
|
274
|
+
);
|
|
275
|
+
if (!owned) {
|
|
276
|
+
throw new RpcError(
|
|
277
|
+
createDiagnostic("MOSTRA_REMOTE_NOT_OWNER_INSTANCE", {
|
|
278
|
+
message: "This instance doesn't share that artifact to this server.",
|
|
279
|
+
}),
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
const version = db.get<{ id: string }>(
|
|
283
|
+
"SELECT id FROM version WHERE id = ? AND artifact_id = ?",
|
|
284
|
+
params.versionId,
|
|
285
|
+
params.artifactId,
|
|
286
|
+
);
|
|
287
|
+
if (!version) throw invalid(`This instance has no version ${params.versionId} to run.`);
|
|
288
|
+
const startedAt = new Date(options.now()).toISOString();
|
|
289
|
+
// No key or viewer here: the hosted home holds the run's key (D130) and its viewer, and a
|
|
290
|
+
// local run's key never meets a hosted one.
|
|
291
|
+
db.run(
|
|
292
|
+
`INSERT INTO action_run (id, artifact_id, version_id, view_id, viewer_id, action, args_json, key,
|
|
293
|
+
client_key, status, started_at) VALUES (?, ?, ?, NULL, NULL, ?, ?, NULL, ?, 'queued', ?)`,
|
|
294
|
+
params.runId,
|
|
295
|
+
params.artifactId,
|
|
296
|
+
params.versionId,
|
|
297
|
+
params.fnId,
|
|
298
|
+
JSON.stringify(params.args),
|
|
299
|
+
`relay:${params.runId}`,
|
|
300
|
+
startedAt,
|
|
301
|
+
);
|
|
302
|
+
// Its start was copied from the hosted home already (the follow can outrun the run): said
|
|
303
|
+
// once. From here on the run's events are this home's own (the follow skips them).
|
|
304
|
+
const copied = db.get(
|
|
305
|
+
"SELECT 1 FROM event WHERE type = 'action.started' AND origin_json IS NOT NULL AND json_extract(payload_json, '$.runId') = ?",
|
|
306
|
+
params.runId,
|
|
307
|
+
);
|
|
308
|
+
if (!copied)
|
|
309
|
+
appendEvent(db, {
|
|
310
|
+
artifactId: params.artifactId,
|
|
311
|
+
type: "action.started",
|
|
312
|
+
actor: { kind: "mostra" },
|
|
313
|
+
causation: { runId: params.runId },
|
|
314
|
+
payload: { runId: params.runId, action: params.fnId, relayed: true, server },
|
|
315
|
+
});
|
|
316
|
+
db.run(
|
|
317
|
+
`INSERT INTO relayed_run (run_id, server, relay_seq, artifact_id, version_id, fn_id, started_at)
|
|
318
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
319
|
+
params.runId,
|
|
320
|
+
server,
|
|
321
|
+
params.seq,
|
|
322
|
+
params.artifactId,
|
|
323
|
+
params.versionId,
|
|
324
|
+
params.fnId,
|
|
325
|
+
startedAt,
|
|
326
|
+
);
|
|
327
|
+
// The highest sequence this ledger accepted: a hosted home restored to before it can tell.
|
|
328
|
+
db.run(
|
|
329
|
+
"UPDATE remote_link SET relay_mark = MAX(relay_mark, ?) WHERE server = ?",
|
|
330
|
+
params.seq,
|
|
331
|
+
server,
|
|
332
|
+
);
|
|
333
|
+
return false;
|
|
334
|
+
});
|
|
335
|
+
if (repeated) {
|
|
336
|
+
// A repeat joins the run: nothing starts again. It reports the run's whole state now, from
|
|
337
|
+
// this ledger, its end too if it ended, so updates lost with the acknowledgement are made up
|
|
338
|
+
// (Opus review of WP-29, 4).
|
|
339
|
+
const state = fullState(params.runId);
|
|
340
|
+
if (state) update(params.runId, session, state);
|
|
341
|
+
return { accepted: true };
|
|
342
|
+
}
|
|
343
|
+
options.actions.runRelayed(params.runId, {
|
|
344
|
+
viewer: params.viewer,
|
|
345
|
+
viewId: params.viewId,
|
|
346
|
+
observer: observer(params.runId, session),
|
|
347
|
+
effect: effectOf(session, params.artifactId, { runId: params.runId }),
|
|
348
|
+
});
|
|
349
|
+
if (loseAcks > 0) {
|
|
350
|
+
loseAcks -= 1;
|
|
351
|
+
log.warn("relay.test_ack_lost", { runId: params.runId });
|
|
352
|
+
drop();
|
|
353
|
+
throw invalid("A test lost this acknowledgement.");
|
|
354
|
+
}
|
|
355
|
+
return { accepted: true };
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
/** Each run the hosted home asks about, from what it holds on. */
|
|
359
|
+
const reconcile = (params: PortRelayReconcileParams): PortRelayReconcileResult => {
|
|
360
|
+
const held = new Map((params.held ?? []).map((entry) => [entry.runId, entry]));
|
|
361
|
+
return {
|
|
362
|
+
runs: ledger.read((db) =>
|
|
363
|
+
params.runs.map((runId) => {
|
|
364
|
+
const row = db.get<RunRow>(
|
|
365
|
+
"SELECT a.* FROM action_run a JOIN relayed_run r ON r.run_id = a.id WHERE a.id = ?",
|
|
366
|
+
runId,
|
|
367
|
+
);
|
|
368
|
+
if (!row) return { runId, state: "unknown-to-owner" as const };
|
|
369
|
+
const position = held.get(runId);
|
|
370
|
+
return {
|
|
371
|
+
runId,
|
|
372
|
+
state: runState(
|
|
373
|
+
db,
|
|
374
|
+
row,
|
|
375
|
+
undefined,
|
|
376
|
+
position ? { log: position.log, batches: position.batches } : undefined,
|
|
377
|
+
),
|
|
378
|
+
};
|
|
379
|
+
}),
|
|
380
|
+
),
|
|
381
|
+
};
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
/** A relayed plain call: started now, its lines streamed back as `relay/call.batch`. */
|
|
385
|
+
const call = async (
|
|
386
|
+
server: string,
|
|
387
|
+
session: () => BindingSession | undefined | Promise<BindingSession | undefined>,
|
|
388
|
+
params: PortRelayCallParams,
|
|
389
|
+
): Promise<{ accepted: true }> => {
|
|
390
|
+
const owned = ledger.read((db) =>
|
|
391
|
+
db.get<{ artifact_id: string }>(
|
|
392
|
+
"SELECT artifact_id FROM hosted_artifact WHERE artifact_id = ? AND server = ?",
|
|
393
|
+
params.artifactId,
|
|
394
|
+
server,
|
|
395
|
+
),
|
|
396
|
+
);
|
|
397
|
+
if (!owned) {
|
|
398
|
+
throw new RpcError(
|
|
399
|
+
createDiagnostic("MOSTRA_REMOTE_NOT_OWNER_INSTANCE", {
|
|
400
|
+
message: "This instance doesn't share that artifact to this server.",
|
|
401
|
+
}),
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
const abort = new AbortController();
|
|
405
|
+
const lines = await options.actions.callRelayed(
|
|
406
|
+
{
|
|
407
|
+
versionId: params.versionId,
|
|
408
|
+
fnId: params.fnId,
|
|
409
|
+
args: params.args,
|
|
410
|
+
viewer: params.viewer,
|
|
411
|
+
viewId: params.viewId,
|
|
412
|
+
effect: effectOf(session, params.artifactId, { callId: params.callId }),
|
|
413
|
+
},
|
|
414
|
+
abort.signal,
|
|
415
|
+
);
|
|
416
|
+
const inFlight = calls.get(server) ?? new Map<string, AbortController>();
|
|
417
|
+
inFlight.set(params.callId, abort);
|
|
418
|
+
calls.set(server, inFlight);
|
|
419
|
+
void (async () => {
|
|
420
|
+
let seq = 0;
|
|
421
|
+
try {
|
|
422
|
+
for await (const line of lines) {
|
|
423
|
+
const open = await session();
|
|
424
|
+
if (!open) {
|
|
425
|
+
abort.abort();
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
seq += 1;
|
|
429
|
+
await open.call(
|
|
430
|
+
relayCallBatchMethod,
|
|
431
|
+
"batch" in line
|
|
432
|
+
? { callId: params.callId, seq, batches: [line.batch] }
|
|
433
|
+
: { callId: params.callId, seq, end: line.end },
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
} catch (error) {
|
|
437
|
+
log.warn("relay.call_failed", { callId: params.callId, error });
|
|
438
|
+
abort.abort();
|
|
439
|
+
} finally {
|
|
440
|
+
inFlight.delete(params.callId);
|
|
441
|
+
}
|
|
442
|
+
})();
|
|
443
|
+
return { accepted: true };
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
return {
|
|
447
|
+
handles: (method) => RELAY_METHODS.has(method),
|
|
448
|
+
async request(server, session, method, params, drop) {
|
|
449
|
+
switch (method) {
|
|
450
|
+
case portRelayRunMethod.method:
|
|
451
|
+
return run(server, session, portRelayRunMethod.params.parse(params), drop);
|
|
452
|
+
case portRelayLiveMethod.method: {
|
|
453
|
+
const parsed = portRelayLiveMethod.params.parse(params);
|
|
454
|
+
const owned = ledger.read((db) =>
|
|
455
|
+
db.get<{ artifact_id: string }>(
|
|
456
|
+
"SELECT artifact_id FROM hosted_artifact WHERE artifact_id = ? AND server = ?",
|
|
457
|
+
parsed.artifactId,
|
|
458
|
+
server,
|
|
459
|
+
),
|
|
460
|
+
);
|
|
461
|
+
if (!owned) return {};
|
|
462
|
+
for (const key of parsed.keys) {
|
|
463
|
+
const id = `${server} ${parsed.artifactId} ${key}`;
|
|
464
|
+
if (parsed.interest === "stop") interest.delete(id);
|
|
465
|
+
else
|
|
466
|
+
interest.set(id, {
|
|
467
|
+
artifactId: parsed.artifactId,
|
|
468
|
+
key,
|
|
469
|
+
until: options.now() + LIVE_INTEREST_MS,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
options.liveChanged?.();
|
|
473
|
+
return {};
|
|
474
|
+
}
|
|
475
|
+
case portRelayCancelMethod.method: {
|
|
476
|
+
const { runId } = portRelayCancelMethod.params.parse(params);
|
|
477
|
+
options.actions.cancelRelayed(runId);
|
|
478
|
+
return {};
|
|
479
|
+
}
|
|
480
|
+
case portRelayReconcileMethod.method:
|
|
481
|
+
return reconcile(portRelayReconcileMethod.params.parse(params));
|
|
482
|
+
case portRelayCallMethod.method:
|
|
483
|
+
return call(server, session, portRelayCallMethod.params.parse(params));
|
|
484
|
+
default:
|
|
485
|
+
throw invalid(`The relay serves no method ${method}.`);
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
closed(server) {
|
|
489
|
+
for (const abort of calls.get(server)?.values() ?? []) abort.abort();
|
|
490
|
+
calls.delete(server);
|
|
491
|
+
},
|
|
492
|
+
liveInterest: () => interest.values(),
|
|
493
|
+
sendLive(server, session, artifactId, versionId, produced) {
|
|
494
|
+
void (async () => {
|
|
495
|
+
const open = await session();
|
|
496
|
+
if (!open) return;
|
|
497
|
+
try {
|
|
498
|
+
await open.call(relayLiveValueMethod, {
|
|
499
|
+
artifactId,
|
|
500
|
+
versionId,
|
|
501
|
+
key: produced.key,
|
|
502
|
+
...(produced.value !== undefined ? { value: produced.value } : {}),
|
|
503
|
+
...(produced.error !== undefined ? { error: produced.error } : {}),
|
|
504
|
+
at: new Date(options.now()).toISOString(),
|
|
505
|
+
});
|
|
506
|
+
} catch (error) {
|
|
507
|
+
log.warn("relay.live_failed", { server, key: produced.key, error });
|
|
508
|
+
}
|
|
509
|
+
})();
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
}
|