@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,493 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The outbox (IN-12, H-04, D105; FAILURE_MODEL §4): delivers each submission, after it is
|
|
3
|
+
* durable, to every host it was enqueued for, through the host's `submissions` port, and mirrors
|
|
4
|
+
* drafts to hosts with a `drafts` port. Each host's deliveries go out one at a time in submission
|
|
5
|
+
* order; a retryable failure keeps the head pending with backoff (1 s doubling to 5 min) and a host
|
|
6
|
+
* that isn't connected gets its deliveries when it registers again, in order. A host's terminal
|
|
7
|
+
* answer is recorded once and never retried. The delivery row is the state: after a crash, a
|
|
8
|
+
* delivery the host acknowledged but Mostra didn't record goes out again with the same ID.
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
type DeliveryState,
|
|
12
|
+
type DraftMirror,
|
|
13
|
+
MAX_LINE_LENGTH,
|
|
14
|
+
portDraftsMirrorMethod,
|
|
15
|
+
portSubmissionsDeliverMethod,
|
|
16
|
+
type SubmissionEnvelope,
|
|
17
|
+
type SubmissionsDeliverResult,
|
|
18
|
+
} from "@mostra/protocol";
|
|
19
|
+
import type { Faults } from "../faults.ts";
|
|
20
|
+
import type { Ledger, LedgerDb } from "../ledger/index.ts";
|
|
21
|
+
import type { Log } from "../log.ts";
|
|
22
|
+
import { appendEvent } from "./events.ts";
|
|
23
|
+
import { documentFor, liveFields } from "./fields.ts";
|
|
24
|
+
import { buildResults } from "./results.ts";
|
|
25
|
+
import { BUILT_IN_OWNER, type SubmissionRow } from "./submissions.ts";
|
|
26
|
+
|
|
27
|
+
/** How long a host has to answer one port call (FAILURE_MODEL §9). */
|
|
28
|
+
export const PORT_CALL_TIMEOUT_MS = 30_000;
|
|
29
|
+
|
|
30
|
+
/** Room for the JSON-RPC request around an envelope (`jsonrpc`, `id`, `method`, newline). */
|
|
31
|
+
const ENVELOPE_FRAME_SLACK = 1024;
|
|
32
|
+
|
|
33
|
+
/** The first retry's delay; each later one doubles, up to {@link MAX_BACKOFF_MS}. */
|
|
34
|
+
export const FIRST_BACKOFF_MS = 1_000;
|
|
35
|
+
|
|
36
|
+
/** The longest delay between retries (FAILURE_MODEL §4). */
|
|
37
|
+
export const MAX_BACKOFF_MS: number = 5 * 60_000;
|
|
38
|
+
|
|
39
|
+
/** The delay before retry number `attempts` + 1. */
|
|
40
|
+
export function backoffMs(attempts: number): number {
|
|
41
|
+
return Math.min(MAX_BACKOFF_MS, FIRST_BACKOFF_MS * 2 ** Math.max(0, attempts - 1));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The connections that registered ports, as the outbox sees them. */
|
|
45
|
+
export interface HostConnections {
|
|
46
|
+
/** The connections that registered `port`, newest last, with their hosts' client names. */
|
|
47
|
+
registrants(port: string): readonly { readonly session: number; readonly owner: string }[];
|
|
48
|
+
/** Calls a port method on one connection; rejects on a transport failure or the timeout. */
|
|
49
|
+
call(session: number, method: string, params: unknown, timeoutMs: number): Promise<unknown>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Options for {@link Outbox}. */
|
|
53
|
+
export interface OutboxOptions {
|
|
54
|
+
readonly ledger: Ledger;
|
|
55
|
+
readonly log: Log;
|
|
56
|
+
readonly hosts: HostConnections;
|
|
57
|
+
/** The hosts that always provide the submissions port (`HomeConfig.ports`, D105). */
|
|
58
|
+
readonly expected: () => readonly string[];
|
|
59
|
+
readonly faults: Faults;
|
|
60
|
+
/** The clock (tests move it). */
|
|
61
|
+
readonly now?: () => number;
|
|
62
|
+
/** How long a host has to answer a port call; default {@link PORT_CALL_TIMEOUT_MS}. */
|
|
63
|
+
readonly portTimeoutMs?: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface DeliveryRow {
|
|
67
|
+
readonly id: string;
|
|
68
|
+
readonly submission_id: string;
|
|
69
|
+
readonly position: number;
|
|
70
|
+
readonly port_owner: string;
|
|
71
|
+
readonly state: DeliveryState;
|
|
72
|
+
readonly attempts: number;
|
|
73
|
+
readonly next_attempt_at: string | null;
|
|
74
|
+
readonly last_error_json: string | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type Outcome =
|
|
78
|
+
| { readonly kind: "delivered"; readonly reason?: string }
|
|
79
|
+
| {
|
|
80
|
+
readonly kind: "terminal";
|
|
81
|
+
readonly state: "rejected" | "undeliverable";
|
|
82
|
+
readonly reason: string;
|
|
83
|
+
}
|
|
84
|
+
| { readonly kind: "retry"; readonly error: string };
|
|
85
|
+
|
|
86
|
+
/** The outbox of one instance. */
|
|
87
|
+
export class Outbox {
|
|
88
|
+
readonly #options: OutboxOptions;
|
|
89
|
+
readonly #now: () => number;
|
|
90
|
+
readonly #running = new Set<string>();
|
|
91
|
+
readonly #again = new Set<string>();
|
|
92
|
+
readonly #timers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
93
|
+
readonly #mirroring = new Map<string, { dirty: boolean }>();
|
|
94
|
+
#unsubscribe: (() => void) | undefined;
|
|
95
|
+
#stopped = false;
|
|
96
|
+
|
|
97
|
+
constructor(options: OutboxOptions) {
|
|
98
|
+
this.#options = options;
|
|
99
|
+
this.#now = options.now ?? Date.now;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The hosts a submission made now is delivered to: registered or expected (D105). */
|
|
103
|
+
owners(): string[] {
|
|
104
|
+
const registered = this.#options.hosts
|
|
105
|
+
.registrants("submissions")
|
|
106
|
+
.map((registrant) => registrant.owner);
|
|
107
|
+
return [...new Set([...registered, ...this.#options.expected()])].sort();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Starts delivering: every commit may have enqueued something. */
|
|
111
|
+
start(): void {
|
|
112
|
+
this.#unsubscribe = this.#options.ledger.onCommit(() => this.kick());
|
|
113
|
+
this.kick();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Stops delivering; a call in flight finishes but nothing new starts. */
|
|
117
|
+
stop(): void {
|
|
118
|
+
this.#stopped = true;
|
|
119
|
+
this.#unsubscribe?.();
|
|
120
|
+
for (const timer of this.#timers.values()) clearTimeout(timer);
|
|
121
|
+
this.#timers.clear();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A host registered its submissions port (again): its pending deliveries are due now, and replay
|
|
126
|
+
* in submission order (FAILURE_MODEL §4).
|
|
127
|
+
*/
|
|
128
|
+
registered(owner: string): void {
|
|
129
|
+
const now = new Date(this.#now()).toISOString();
|
|
130
|
+
this.#options.ledger.write((db) =>
|
|
131
|
+
db.run(
|
|
132
|
+
"UPDATE delivery SET next_attempt_at = ? WHERE port_owner = ? AND state = 'pending'",
|
|
133
|
+
now,
|
|
134
|
+
owner,
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
this.kick();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Looks for work for every host with pending deliveries. */
|
|
141
|
+
kick(): void {
|
|
142
|
+
if (this.#stopped) return;
|
|
143
|
+
const owners = this.#options.ledger.read((db) =>
|
|
144
|
+
db.all<{ port_owner: string }>(
|
|
145
|
+
"SELECT DISTINCT port_owner FROM delivery WHERE state = 'pending'",
|
|
146
|
+
),
|
|
147
|
+
);
|
|
148
|
+
for (const { port_owner: owner } of owners) void this.#pump(owner);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Each host's deliveries by state, for `instance/status`. */
|
|
152
|
+
counts(): {
|
|
153
|
+
owner: string;
|
|
154
|
+
connected: boolean;
|
|
155
|
+
pending: number;
|
|
156
|
+
delivered: number;
|
|
157
|
+
rejected: number;
|
|
158
|
+
undeliverable: number;
|
|
159
|
+
}[] {
|
|
160
|
+
const rows = this.#options.ledger.read((db) =>
|
|
161
|
+
db.all<{ port_owner: string; state: DeliveryState; count: number }>(
|
|
162
|
+
"SELECT port_owner, state, COUNT(*) AS count FROM delivery GROUP BY port_owner, state",
|
|
163
|
+
),
|
|
164
|
+
);
|
|
165
|
+
const connected = new Set(
|
|
166
|
+
this.#options.hosts.registrants("submissions").map((registrant) => registrant.owner),
|
|
167
|
+
);
|
|
168
|
+
const byOwner = new Map<
|
|
169
|
+
string,
|
|
170
|
+
{
|
|
171
|
+
owner: string;
|
|
172
|
+
connected: boolean;
|
|
173
|
+
pending: number;
|
|
174
|
+
delivered: number;
|
|
175
|
+
rejected: number;
|
|
176
|
+
undeliverable: number;
|
|
177
|
+
}
|
|
178
|
+
>();
|
|
179
|
+
for (const row of rows) {
|
|
180
|
+
const entry = byOwner.get(row.port_owner) ?? {
|
|
181
|
+
owner: row.port_owner,
|
|
182
|
+
connected: row.port_owner === BUILT_IN_OWNER || connected.has(row.port_owner),
|
|
183
|
+
pending: 0,
|
|
184
|
+
delivered: 0,
|
|
185
|
+
rejected: 0,
|
|
186
|
+
undeliverable: 0,
|
|
187
|
+
};
|
|
188
|
+
entry[row.state] = row.count;
|
|
189
|
+
byOwner.set(row.port_owner, entry);
|
|
190
|
+
}
|
|
191
|
+
return [...byOwner.values()].sort((a, b) => (a.owner < b.owner ? -1 : 1));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async #pump(owner: string): Promise<void> {
|
|
195
|
+
if (this.#running.has(owner)) {
|
|
196
|
+
this.#again.add(owner);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
this.#running.add(owner);
|
|
200
|
+
try {
|
|
201
|
+
do {
|
|
202
|
+
this.#again.delete(owner);
|
|
203
|
+
while (!this.#stopped && (await this.#step(owner))) {
|
|
204
|
+
// One delivery at a time, in order.
|
|
205
|
+
}
|
|
206
|
+
} while (this.#again.has(owner) && !this.#stopped);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
this.#options.log.error("outbox.failed", { owner, error });
|
|
209
|
+
} finally {
|
|
210
|
+
this.#running.delete(owner);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Delivers the owner's next pending delivery; `false` when there is nothing to do now. */
|
|
215
|
+
async #step(owner: string): Promise<boolean> {
|
|
216
|
+
const { ledger, hosts, log, faults } = this.#options;
|
|
217
|
+
const head = ledger.read((db) =>
|
|
218
|
+
db.get<DeliveryRow>(
|
|
219
|
+
"SELECT * FROM delivery WHERE port_owner = ? AND state = 'pending' ORDER BY position LIMIT 1",
|
|
220
|
+
owner,
|
|
221
|
+
),
|
|
222
|
+
);
|
|
223
|
+
if (!head) return false;
|
|
224
|
+
if (owner === BUILT_IN_OWNER) {
|
|
225
|
+
this.#record(head, { kind: "delivered" });
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
const due = head.next_attempt_at === null ? 0 : Date.parse(head.next_attempt_at);
|
|
229
|
+
const wait = due - this.#now();
|
|
230
|
+
if (wait > 0) {
|
|
231
|
+
this.#schedule(owner, wait);
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
const registrant = hosts
|
|
235
|
+
.registrants("submissions")
|
|
236
|
+
.filter((entry) => entry.owner === owner)
|
|
237
|
+
.at(-1);
|
|
238
|
+
// Not connected: its deliveries wait for it to register again, and the viewer learns that
|
|
239
|
+
// the submission is waiting (IN-13).
|
|
240
|
+
if (!registrant) {
|
|
241
|
+
if (head.last_error_json === null) this.#waiting(head, "The host isn't connected.");
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
const attempt = head.attempts + 1;
|
|
245
|
+
const envelope = ledger.write((db) => {
|
|
246
|
+
db.run(
|
|
247
|
+
"UPDATE delivery SET attempts = ?, updated_at = ? WHERE id = ?",
|
|
248
|
+
attempt,
|
|
249
|
+
new Date(this.#now()).toISOString(),
|
|
250
|
+
head.id,
|
|
251
|
+
);
|
|
252
|
+
const submission = db.get<SubmissionRow>(
|
|
253
|
+
"SELECT * FROM submission WHERE id = ?",
|
|
254
|
+
head.submission_id,
|
|
255
|
+
) as SubmissionRow;
|
|
256
|
+
const envelope: SubmissionEnvelope = {
|
|
257
|
+
kind: "submission",
|
|
258
|
+
deliveryId: head.id,
|
|
259
|
+
submissionId: submission.id,
|
|
260
|
+
artifactId: submission.artifact_id,
|
|
261
|
+
attempt,
|
|
262
|
+
results: buildResults(db, submission),
|
|
263
|
+
};
|
|
264
|
+
return envelope;
|
|
265
|
+
});
|
|
266
|
+
let outcome: Outcome;
|
|
267
|
+
// An envelope no host could read would be retried forever and hold back every later delivery
|
|
268
|
+
// to this host: it is undeliverable, with the reason, and never sent (Claude review finding 2).
|
|
269
|
+
const length = JSON.stringify(envelope).length + ENVELOPE_FRAME_SLACK;
|
|
270
|
+
if (length > MAX_LINE_LENGTH) {
|
|
271
|
+
log.error("outbox.too_large", { owner, deliveryId: head.id, length });
|
|
272
|
+
this.#record(
|
|
273
|
+
{ ...head, attempts: attempt },
|
|
274
|
+
{
|
|
275
|
+
kind: "terminal",
|
|
276
|
+
state: "undeliverable",
|
|
277
|
+
reason: `too-large: the submission's envelope is ${length} characters; one protocol message carries ${MAX_LINE_LENGTH}`,
|
|
278
|
+
},
|
|
279
|
+
);
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
const answer = await hosts.call(
|
|
284
|
+
registrant.session,
|
|
285
|
+
portSubmissionsDeliverMethod.method,
|
|
286
|
+
envelope,
|
|
287
|
+
this.#options.portTimeoutMs ?? PORT_CALL_TIMEOUT_MS,
|
|
288
|
+
);
|
|
289
|
+
const parsed = portSubmissionsDeliverMethod.result.safeParse(answer);
|
|
290
|
+
outcome = parsed.success
|
|
291
|
+
? outcomeOf(parsed.data)
|
|
292
|
+
: {
|
|
293
|
+
kind: "retry",
|
|
294
|
+
error: "The host's answer doesn't match port/submissions.deliver's result.",
|
|
295
|
+
};
|
|
296
|
+
} catch (error) {
|
|
297
|
+
outcome = { kind: "retry", error: (error as Error).message };
|
|
298
|
+
}
|
|
299
|
+
// A crash here, after the host answered and before Mostra records it, redelivers the same
|
|
300
|
+
// delivery ID on the next start (FAILURE_MODEL §4).
|
|
301
|
+
faults.at("deliver:after-answer");
|
|
302
|
+
this.#record({ ...head, attempts: attempt }, outcome);
|
|
303
|
+
if (outcome.kind === "retry") {
|
|
304
|
+
log.warn("outbox.retry", { owner, deliveryId: head.id, attempt, error: outcome.error });
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
#schedule(owner: string, delayMs: number): void {
|
|
311
|
+
if (this.#timers.has(owner) || this.#stopped) return;
|
|
312
|
+
const timer = setTimeout(() => {
|
|
313
|
+
this.#timers.delete(owner);
|
|
314
|
+
void this.#pump(owner);
|
|
315
|
+
}, delayMs);
|
|
316
|
+
timer.unref?.();
|
|
317
|
+
this.#timers.set(owner, timer);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
#record(head: DeliveryRow, outcome: Outcome): void {
|
|
321
|
+
const now = new Date(this.#now()).toISOString();
|
|
322
|
+
const { ledger } = this.#options;
|
|
323
|
+
ledger.write((db) => {
|
|
324
|
+
const current = db.get<{ state: DeliveryState }>(
|
|
325
|
+
"SELECT state FROM delivery WHERE id = ?",
|
|
326
|
+
head.id,
|
|
327
|
+
);
|
|
328
|
+
if (current?.state !== "pending") return;
|
|
329
|
+
const submission = db.get<{ artifact_id: string }>(
|
|
330
|
+
"SELECT artifact_id FROM submission WHERE id = ?",
|
|
331
|
+
head.submission_id,
|
|
332
|
+
) as { artifact_id: string };
|
|
333
|
+
if (outcome.kind === "retry") {
|
|
334
|
+
const first = db.get<{ last_error_json: string | null }>(
|
|
335
|
+
"SELECT last_error_json FROM delivery WHERE id = ?",
|
|
336
|
+
head.id,
|
|
337
|
+
);
|
|
338
|
+
db.run(
|
|
339
|
+
"UPDATE delivery SET next_attempt_at = ?, last_error_json = ?, updated_at = ? WHERE id = ?",
|
|
340
|
+
new Date(this.#now() + backoffMs(head.attempts)).toISOString(),
|
|
341
|
+
JSON.stringify({ message: outcome.error.slice(0, 500), at: now }),
|
|
342
|
+
now,
|
|
343
|
+
head.id,
|
|
344
|
+
);
|
|
345
|
+
if (first?.last_error_json === null) {
|
|
346
|
+
appendRetrying(db, submission.artifact_id, head, outcome.error);
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const state = outcome.kind === "delivered" ? "delivered" : outcome.state;
|
|
351
|
+
const reason = outcome.reason ?? null;
|
|
352
|
+
db.run(
|
|
353
|
+
"UPDATE delivery SET state = ?, reason = ?, next_attempt_at = NULL, updated_at = ? WHERE id = ?",
|
|
354
|
+
state,
|
|
355
|
+
reason,
|
|
356
|
+
now,
|
|
357
|
+
head.id,
|
|
358
|
+
);
|
|
359
|
+
appendEvent(db, {
|
|
360
|
+
artifactId: submission.artifact_id,
|
|
361
|
+
type: `submission.${state}`,
|
|
362
|
+
actor:
|
|
363
|
+
head.port_owner === BUILT_IN_OWNER
|
|
364
|
+
? { kind: "mostra" }
|
|
365
|
+
: { kind: "host", id: head.port_owner },
|
|
366
|
+
causation: { submissionId: head.submission_id },
|
|
367
|
+
payload: {
|
|
368
|
+
submissionId: head.submission_id,
|
|
369
|
+
deliveryId: head.id,
|
|
370
|
+
owner: head.port_owner,
|
|
371
|
+
attempts: head.attempts,
|
|
372
|
+
...(reason !== null ? { reason } : {}),
|
|
373
|
+
},
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Records that a delivery waits for a host that isn't connected, once: the viewer sees "Pending
|
|
380
|
+
* delivery" instead of a submission that looks merely slow (IN-13, D47).
|
|
381
|
+
*/
|
|
382
|
+
#waiting(head: DeliveryRow, reason: string): void {
|
|
383
|
+
const now = new Date(this.#now()).toISOString();
|
|
384
|
+
this.#options.ledger.write((db) => {
|
|
385
|
+
const current = db.get<{ state: DeliveryState; last_error_json: string | null }>(
|
|
386
|
+
"SELECT state, last_error_json FROM delivery WHERE id = ?",
|
|
387
|
+
head.id,
|
|
388
|
+
);
|
|
389
|
+
if (current?.state !== "pending" || current.last_error_json !== null) return;
|
|
390
|
+
db.run(
|
|
391
|
+
"UPDATE delivery SET last_error_json = ?, updated_at = ? WHERE id = ?",
|
|
392
|
+
JSON.stringify({ message: reason, at: now }),
|
|
393
|
+
now,
|
|
394
|
+
head.id,
|
|
395
|
+
);
|
|
396
|
+
const submission = db.get<{ artifact_id: string }>(
|
|
397
|
+
"SELECT artifact_id FROM submission WHERE id = ?",
|
|
398
|
+
head.submission_id,
|
|
399
|
+
) as { artifact_id: string };
|
|
400
|
+
appendRetrying(db, submission.artifact_id, head, reason);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Mirrors a viewer's draft to every connection with a `drafts` port (D105): best effort, the
|
|
406
|
+
* latest revision only, coalesced while one is in flight, never retried. A mirror is labelled
|
|
407
|
+
* `kind: "draft"` and never goes through the submissions port (IN-06).
|
|
408
|
+
*/
|
|
409
|
+
mirror(artifactId: string, viewerId: string): void {
|
|
410
|
+
for (const registrant of this.#options.hosts.registrants("drafts")) {
|
|
411
|
+
const key = `${registrant.session} ${artifactId} ${viewerId}`;
|
|
412
|
+
const running = this.#mirroring.get(key);
|
|
413
|
+
if (running) {
|
|
414
|
+
running.dirty = true;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const entry = { dirty: false };
|
|
418
|
+
this.#mirroring.set(key, entry);
|
|
419
|
+
void (async () => {
|
|
420
|
+
try {
|
|
421
|
+
do {
|
|
422
|
+
entry.dirty = false;
|
|
423
|
+
const mirror = this.#options.ledger.read((db): DraftMirror | undefined => {
|
|
424
|
+
const draft = documentFor(db, artifactId, viewerId);
|
|
425
|
+
if (!draft) return undefined;
|
|
426
|
+
const fields = liveFields(db, draft.id);
|
|
427
|
+
return {
|
|
428
|
+
kind: "draft",
|
|
429
|
+
artifactId,
|
|
430
|
+
viewerId,
|
|
431
|
+
epoch: draft.epoch,
|
|
432
|
+
revision: String(draft.revision),
|
|
433
|
+
fields: Object.fromEntries(
|
|
434
|
+
Object.entries(fields).map(([id, field]) => [id, field.value]),
|
|
435
|
+
),
|
|
436
|
+
};
|
|
437
|
+
});
|
|
438
|
+
if (!mirror || this.#stopped) break;
|
|
439
|
+
try {
|
|
440
|
+
await this.#options.hosts.call(
|
|
441
|
+
registrant.session,
|
|
442
|
+
portDraftsMirrorMethod.method,
|
|
443
|
+
mirror,
|
|
444
|
+
this.#options.portTimeoutMs ?? PORT_CALL_TIMEOUT_MS,
|
|
445
|
+
);
|
|
446
|
+
} catch (error) {
|
|
447
|
+
this.#options.log.warn("outbox.mirror_failed", {
|
|
448
|
+
artifactId,
|
|
449
|
+
owner: registrant.owner,
|
|
450
|
+
error: (error as Error).message,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
} while (entry.dirty && !this.#stopped);
|
|
454
|
+
} finally {
|
|
455
|
+
this.#mirroring.delete(key);
|
|
456
|
+
}
|
|
457
|
+
})();
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* `submission.retrying`: a delivery is waiting, its host away or its first attempt failed (D47's
|
|
464
|
+
* "Pending delivery"). Appended once per delivery, so a long outage never fills the event log;
|
|
465
|
+
* `submission.delivered` and the terminal events end it.
|
|
466
|
+
*/
|
|
467
|
+
function appendRetrying(db: LedgerDb, artifactId: string, head: DeliveryRow, reason: string): void {
|
|
468
|
+
appendEvent(db, {
|
|
469
|
+
artifactId,
|
|
470
|
+
type: "submission.retrying",
|
|
471
|
+
actor: { kind: "mostra" },
|
|
472
|
+
causation: { submissionId: head.submission_id },
|
|
473
|
+
payload: {
|
|
474
|
+
submissionId: head.submission_id,
|
|
475
|
+
deliveryId: head.id,
|
|
476
|
+
owner: head.port_owner,
|
|
477
|
+
attempts: head.attempts,
|
|
478
|
+
reason: reason.slice(0, 500),
|
|
479
|
+
},
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** What a host's answer means (D105). */
|
|
484
|
+
function outcomeOf(answer: SubmissionsDeliverResult): Outcome {
|
|
485
|
+
if (answer.accepted) return { kind: "delivered" };
|
|
486
|
+
if (answer.retry) return { kind: "retry", error: `The host asked to retry: ${answer.reason}` };
|
|
487
|
+
if (answer.reason === "duplicate") return { kind: "delivered", reason: "duplicate" };
|
|
488
|
+
return {
|
|
489
|
+
kind: "terminal",
|
|
490
|
+
state: answer.reason === "undeliverable" ? "undeliverable" : "rejected",
|
|
491
|
+
reason: answer.reason,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The submission policy in force (D10, D39, D63, D102): the first of the publish's `policy`, the
|
|
3
|
+
* home's `HomeConfig.submission` and the manifest's request, else `once` per-artifact. It is
|
|
4
|
+
* resolved at publish and stored on the artifact; `once` always carries its scope.
|
|
5
|
+
*/
|
|
6
|
+
import type { SubmissionPolicy } from "@mostra/protocol";
|
|
7
|
+
|
|
8
|
+
/** The policy when nobody asked for one: exactly one submission per artifact (D39). */
|
|
9
|
+
export const DEFAULT_POLICY: SubmissionPolicy = { policy: "once", scope: "per-artifact" };
|
|
10
|
+
|
|
11
|
+
/** The policy in force, from the sources in D102's order, with `once`'s scope filled in. */
|
|
12
|
+
export function policyInForce(
|
|
13
|
+
...sources: readonly (SubmissionPolicy | undefined)[]
|
|
14
|
+
): SubmissionPolicy {
|
|
15
|
+
const chosen = sources.find((source) => source !== undefined) ?? DEFAULT_POLICY;
|
|
16
|
+
return chosen.policy === "once"
|
|
17
|
+
? { policy: "once", scope: chosen.scope ?? "per-artifact" }
|
|
18
|
+
: { policy: chosen.policy };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The stored policy of an artifact row; artifacts published before WP-06 get the default. */
|
|
22
|
+
export function storedPolicy(policyJson: string | null): SubmissionPolicy {
|
|
23
|
+
return policyInForce(
|
|
24
|
+
policyJson === null ? undefined : (JSON.parse(policyJson) as SubmissionPolicy),
|
|
25
|
+
);
|
|
26
|
+
}
|