@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,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signals (IN-09, D10, D106): named, non-terminal requests for a host's attention, raised by the
|
|
3
|
+
* artifact (`useSignal`), by server code (`signal()`, WP-08) or by a submission (`submit`). Each
|
|
4
|
+
* is one durable `signal.raised` event and nothing more: Mostra never decides on its own what a
|
|
5
|
+
* signal wakes. Hosts filter events and decide.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
type Actor,
|
|
9
|
+
canonicalJson,
|
|
10
|
+
createDiagnostic,
|
|
11
|
+
RpcError,
|
|
12
|
+
SIGNAL_NAME_PATTERN,
|
|
13
|
+
SIGNAL_PAYLOAD_MAX_BYTES,
|
|
14
|
+
} from "@mostra/protocol";
|
|
15
|
+
import type { LedgerDb } from "../ledger/index.ts";
|
|
16
|
+
import { appendEvent } from "./events.ts";
|
|
17
|
+
|
|
18
|
+
/** Who raised a signal (DATA_MODEL §4). */
|
|
19
|
+
export type SignalSource = "shell" | "artifact" | "action";
|
|
20
|
+
|
|
21
|
+
/** The signal every submission raises (D106); no one else may raise it. */
|
|
22
|
+
export const SUBMIT_SIGNAL = "submit";
|
|
23
|
+
|
|
24
|
+
function invalid(message: string): RpcError {
|
|
25
|
+
return new RpcError(createDiagnostic("MOSTRA_SUBMIT_SIGNAL_INVALID", { message }));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Checks a signal's name and payload (D106). */
|
|
29
|
+
export function checkSignal(name: unknown, payload: unknown): void {
|
|
30
|
+
if (typeof name !== "string" || !SIGNAL_NAME_PATTERN.test(name)) {
|
|
31
|
+
throw invalid(
|
|
32
|
+
`${JSON.stringify(name).slice(0, 80)} isn't a signal name: 1 to 64 letters, digits, ".", "_" and "-".`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
if (name === SUBMIT_SIGNAL) {
|
|
36
|
+
throw invalid('The signal name "submit" is reserved: a submission raises it.');
|
|
37
|
+
}
|
|
38
|
+
if (payload === undefined) return;
|
|
39
|
+
let text: string;
|
|
40
|
+
try {
|
|
41
|
+
text = canonicalJson(payload);
|
|
42
|
+
} catch {
|
|
43
|
+
throw invalid(`The payload of the signal ${name} isn't JSON.`);
|
|
44
|
+
}
|
|
45
|
+
if (Buffer.byteLength(text, "utf8") > SIGNAL_PAYLOAD_MAX_BYTES) {
|
|
46
|
+
throw invalid(`The payload of the signal ${name} is larger than 64 KiB of JSON.`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Appends `signal.raised` inside the caller's transaction and returns its cursor, after checking
|
|
52
|
+
* the name and payload. No caller may raise `submit`: only a submission does (D106).
|
|
53
|
+
*/
|
|
54
|
+
export function raiseSignal(
|
|
55
|
+
db: LedgerDb,
|
|
56
|
+
input: {
|
|
57
|
+
readonly artifactId: string;
|
|
58
|
+
readonly name: string;
|
|
59
|
+
readonly payload?: unknown;
|
|
60
|
+
readonly source: SignalSource;
|
|
61
|
+
readonly actor: Actor;
|
|
62
|
+
readonly viewId?: string;
|
|
63
|
+
/** The viewer whose call raised it, for a signal of server code (D104: theirs alone). */
|
|
64
|
+
readonly viewerId?: string;
|
|
65
|
+
readonly causation?: Readonly<Record<string, string>>;
|
|
66
|
+
},
|
|
67
|
+
): string {
|
|
68
|
+
checkSignal(input.name, input.payload);
|
|
69
|
+
return appendSignal(db, input);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The `submit` signal a submission raises, in its transaction (D106). */
|
|
73
|
+
export function raiseSubmitSignal(
|
|
74
|
+
db: LedgerDb,
|
|
75
|
+
input: {
|
|
76
|
+
readonly artifactId: string;
|
|
77
|
+
readonly submissionId: string;
|
|
78
|
+
readonly channel: "shell" | "artifact";
|
|
79
|
+
readonly actor: Actor;
|
|
80
|
+
readonly viewId: string;
|
|
81
|
+
},
|
|
82
|
+
): string {
|
|
83
|
+
return appendSignal(db, {
|
|
84
|
+
artifactId: input.artifactId,
|
|
85
|
+
name: SUBMIT_SIGNAL,
|
|
86
|
+
payload: { submissionId: input.submissionId },
|
|
87
|
+
source: input.channel,
|
|
88
|
+
actor: input.actor,
|
|
89
|
+
viewId: input.viewId,
|
|
90
|
+
causation: { submissionId: input.submissionId },
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function appendSignal(
|
|
95
|
+
db: LedgerDb,
|
|
96
|
+
input: {
|
|
97
|
+
readonly artifactId: string;
|
|
98
|
+
readonly name: string;
|
|
99
|
+
readonly payload?: unknown;
|
|
100
|
+
readonly source: SignalSource;
|
|
101
|
+
readonly actor: Actor;
|
|
102
|
+
readonly viewId?: string;
|
|
103
|
+
readonly viewerId?: string;
|
|
104
|
+
readonly causation?: Readonly<Record<string, string>>;
|
|
105
|
+
},
|
|
106
|
+
): string {
|
|
107
|
+
return appendEvent(db, {
|
|
108
|
+
artifactId: input.artifactId,
|
|
109
|
+
type: "signal.raised",
|
|
110
|
+
actor: input.actor,
|
|
111
|
+
...(input.causation ? { causation: input.causation } : {}),
|
|
112
|
+
payload: {
|
|
113
|
+
name: input.name,
|
|
114
|
+
source: input.source,
|
|
115
|
+
...(input.viewId !== undefined ? { viewId: input.viewId } : {}),
|
|
116
|
+
...(input.viewerId !== undefined ? { viewerId: input.viewerId } : {}),
|
|
117
|
+
...(input.payload !== undefined ? { payload: input.payload } : {}),
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The submit transaction (IN-05, IN-07, IN-12; ARCHITECTURE §6, D39, D40, D103, D106). One ledger
|
|
3
|
+
* transaction resolves the view to its exact version, checks the policy in force and the draft
|
|
4
|
+
* revision the viewer saw, reads the canonical persisted draft and artifact state, computes the
|
|
5
|
+
* snapshot's digest, inserts the immutable submission, enqueues one delivery per host, and
|
|
6
|
+
* appends `submission.created` and the `submit` signal. A submission is never made from content
|
|
7
|
+
* the page sent: only from what the ledger holds (IN-06).
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import {
|
|
11
|
+
canonicalJson,
|
|
12
|
+
createDiagnostic,
|
|
13
|
+
DeliveryId,
|
|
14
|
+
type DeliveryState,
|
|
15
|
+
type DeliveryStatus,
|
|
16
|
+
type OrphanedField,
|
|
17
|
+
RpcError,
|
|
18
|
+
SubmissionId,
|
|
19
|
+
type SubmissionPolicy,
|
|
20
|
+
type SubmissionSummary,
|
|
21
|
+
type SubmitChannel,
|
|
22
|
+
type Viewer,
|
|
23
|
+
} from "@mostra/protocol";
|
|
24
|
+
import type { LedgerDb } from "../ledger/index.ts";
|
|
25
|
+
import { appendEvent } from "./events.ts";
|
|
26
|
+
import { documentFor, type FieldRow } from "./fields.ts";
|
|
27
|
+
import { storedPolicy } from "./policy.ts";
|
|
28
|
+
import { raiseSubmitSignal, SUBMIT_SIGNAL } from "./signals.ts";
|
|
29
|
+
import { type ViewRow, viewControls, viewer as viewerById, viewFor } from "./viewers.ts";
|
|
30
|
+
|
|
31
|
+
/** The most signals a snapshot keeps: the view's newest. */
|
|
32
|
+
export const SNAPSHOT_SIGNALS_MAX = 100;
|
|
33
|
+
|
|
34
|
+
/** The most signal payload text a snapshot keeps. */
|
|
35
|
+
export const SNAPSHOT_SIGNAL_BYTES_MAX: number = 1024 * 1024;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The longest snapshot, in characters: the two 16 MiB documents, labels and signals fit, and the
|
|
39
|
+
* results built from it stay well inside one 64 Mi-character protocol message.
|
|
40
|
+
*/
|
|
41
|
+
export const SNAPSHOT_MAX_LENGTH: number = 40 * 1024 * 1024;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The owner of the built-in adapter's deliveries, which accepts at once (D58, D105): empty, since
|
|
45
|
+
* a client name never is, so no host can be taken for it.
|
|
46
|
+
*/
|
|
47
|
+
export const BUILT_IN_OWNER = "";
|
|
48
|
+
|
|
49
|
+
/** One field as a snapshot keeps it. */
|
|
50
|
+
export interface SnapshotField {
|
|
51
|
+
readonly value: unknown;
|
|
52
|
+
readonly label?: string;
|
|
53
|
+
readonly required?: boolean;
|
|
54
|
+
readonly sensitive?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The canonical answer document of a submission (DATA_MODEL §3 `snapshot_json`): everything the
|
|
59
|
+
* results are made of, frozen at submit. Its digest is the SHA-256 of its RFC 8785 form.
|
|
60
|
+
*/
|
|
61
|
+
export interface Snapshot {
|
|
62
|
+
readonly schema: 1;
|
|
63
|
+
readonly submissionId: string;
|
|
64
|
+
readonly at: string;
|
|
65
|
+
readonly artifactId: string;
|
|
66
|
+
readonly versionId: string;
|
|
67
|
+
readonly version: number;
|
|
68
|
+
readonly viewId: string;
|
|
69
|
+
readonly viewer: Viewer;
|
|
70
|
+
readonly channel: SubmitChannel;
|
|
71
|
+
readonly policy: SubmissionPolicy;
|
|
72
|
+
readonly epoch: number;
|
|
73
|
+
readonly previous?: string;
|
|
74
|
+
readonly staleView: boolean;
|
|
75
|
+
readonly latestVersion?: number;
|
|
76
|
+
readonly draft: { readonly revision: string; readonly fields: Record<string, SnapshotField> };
|
|
77
|
+
readonly state?: { readonly revision: string; readonly fields: Record<string, SnapshotField> };
|
|
78
|
+
readonly orphaned: readonly {
|
|
79
|
+
readonly id: string;
|
|
80
|
+
readonly label?: string;
|
|
81
|
+
readonly value: unknown;
|
|
82
|
+
readonly fromVersion: number;
|
|
83
|
+
}[];
|
|
84
|
+
readonly signals: readonly {
|
|
85
|
+
readonly name: string;
|
|
86
|
+
readonly at: string;
|
|
87
|
+
readonly payload?: unknown;
|
|
88
|
+
}[];
|
|
89
|
+
readonly signalsOmitted?: number;
|
|
90
|
+
readonly signalsCompacted?: true;
|
|
91
|
+
/**
|
|
92
|
+
* The action runs started in the view before the submission, with their state then (WP-08):
|
|
93
|
+
* at most 100, oldest first. Snapshots made before WP-08 have none.
|
|
94
|
+
*/
|
|
95
|
+
readonly actions?: readonly {
|
|
96
|
+
readonly runId: string;
|
|
97
|
+
readonly action: string;
|
|
98
|
+
readonly status: string;
|
|
99
|
+
readonly at: string;
|
|
100
|
+
}[];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The most action runs a snapshot lists. */
|
|
104
|
+
const SNAPSHOT_ACTIONS_MAX = 100;
|
|
105
|
+
|
|
106
|
+
/** A `submission` row. */
|
|
107
|
+
export interface SubmissionRow {
|
|
108
|
+
readonly id: string;
|
|
109
|
+
readonly position: number;
|
|
110
|
+
readonly artifact_id: string;
|
|
111
|
+
readonly version_id: string;
|
|
112
|
+
readonly view_id: string;
|
|
113
|
+
readonly viewer_id: string;
|
|
114
|
+
readonly channel: SubmitChannel;
|
|
115
|
+
readonly policy_json: string;
|
|
116
|
+
readonly epoch: number;
|
|
117
|
+
readonly scope_key: string;
|
|
118
|
+
readonly previous_id: string | null;
|
|
119
|
+
readonly draft_document_id: number;
|
|
120
|
+
readonly draft_revision: number;
|
|
121
|
+
readonly state_revision: number | null;
|
|
122
|
+
readonly latest_version_number: number | null;
|
|
123
|
+
readonly snapshot_json: string;
|
|
124
|
+
readonly digest: string;
|
|
125
|
+
readonly stale_view: number;
|
|
126
|
+
readonly created_at: string;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
interface DeliveryRow {
|
|
130
|
+
readonly id: string;
|
|
131
|
+
readonly port_owner: string;
|
|
132
|
+
readonly state: DeliveryState;
|
|
133
|
+
readonly attempts: number;
|
|
134
|
+
readonly reason: string | null;
|
|
135
|
+
readonly last_error_json: string | null;
|
|
136
|
+
readonly updated_at: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A refused submit that carries the submission already made (H-02): `MOSTRA_SUBMIT_ALREADY` under
|
|
141
|
+
* `once`. The gateway answers it with the submission beside the diagnostic.
|
|
142
|
+
*/
|
|
143
|
+
export class SubmitConflict extends RpcError {
|
|
144
|
+
readonly submission: SubmissionSummary;
|
|
145
|
+
|
|
146
|
+
constructor(message: string, submission: SubmissionSummary) {
|
|
147
|
+
super(createDiagnostic("MOSTRA_SUBMIT_ALREADY", { message }));
|
|
148
|
+
this.submission = submission;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const SEVERITY: Readonly<Record<DeliveryState, number>> = {
|
|
153
|
+
delivered: 0,
|
|
154
|
+
pending: 1,
|
|
155
|
+
rejected: 2,
|
|
156
|
+
undeliverable: 3,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/** A submission's deliveries. */
|
|
160
|
+
export function deliveriesOf(db: LedgerDb, submissionId: string): DeliveryStatus[] {
|
|
161
|
+
return db
|
|
162
|
+
.all<DeliveryRow>(
|
|
163
|
+
"SELECT * FROM delivery WHERE submission_id = ? ORDER BY port_owner",
|
|
164
|
+
submissionId,
|
|
165
|
+
)
|
|
166
|
+
.map((row) => ({
|
|
167
|
+
deliveryId: row.id,
|
|
168
|
+
owner: row.port_owner,
|
|
169
|
+
state: row.state,
|
|
170
|
+
attempts: row.attempts,
|
|
171
|
+
...(row.reason !== null ? { reason: row.reason } : {}),
|
|
172
|
+
...(row.state === "pending" && row.last_error_json !== null
|
|
173
|
+
? { waiting: true as const }
|
|
174
|
+
: {}),
|
|
175
|
+
updatedAt: row.updated_at,
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Every delivery's state summed up: the worst of them (`delivered` when there are none). */
|
|
180
|
+
export function overallDelivery(deliveries: readonly DeliveryStatus[]): DeliveryState {
|
|
181
|
+
let worst: DeliveryState = "delivered";
|
|
182
|
+
for (const delivery of deliveries)
|
|
183
|
+
if (SEVERITY[delivery.state] > SEVERITY[worst]) worst = delivery.state;
|
|
184
|
+
return worst;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** A submission as a submit answers it. */
|
|
188
|
+
export function submissionSummary(db: LedgerDb, row: SubmissionRow): SubmissionSummary {
|
|
189
|
+
const snapshot = JSON.parse(row.snapshot_json) as Snapshot;
|
|
190
|
+
return {
|
|
191
|
+
id: row.id,
|
|
192
|
+
at: row.created_at,
|
|
193
|
+
epoch: row.epoch,
|
|
194
|
+
channel: row.channel,
|
|
195
|
+
versionId: row.version_id,
|
|
196
|
+
version: snapshot.version,
|
|
197
|
+
viewId: row.view_id,
|
|
198
|
+
staleView: row.stale_view === 1,
|
|
199
|
+
draftRevision: String(row.draft_revision),
|
|
200
|
+
digest: row.digest,
|
|
201
|
+
...(() => {
|
|
202
|
+
const deliveries = deliveriesOf(db, row.id);
|
|
203
|
+
return { delivery: overallDelivery(deliveries), deliveries };
|
|
204
|
+
})(),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The submission that closes a viewer's draft under `once`, if any (D106). */
|
|
209
|
+
export function closingSubmission(
|
|
210
|
+
db: LedgerDb,
|
|
211
|
+
artifactId: string,
|
|
212
|
+
viewerId: string,
|
|
213
|
+
): SubmissionRow | undefined {
|
|
214
|
+
const artifact = db.get<{ policy_json: string | null }>(
|
|
215
|
+
"SELECT policy_json FROM artifact WHERE id = ?",
|
|
216
|
+
artifactId,
|
|
217
|
+
);
|
|
218
|
+
if (!artifact) return undefined;
|
|
219
|
+
const policy = storedPolicy(artifact.policy_json);
|
|
220
|
+
if (policy.policy !== "once") return undefined;
|
|
221
|
+
return policy.scope === "per-viewer"
|
|
222
|
+
? db.get<SubmissionRow>(
|
|
223
|
+
"SELECT * FROM submission WHERE artifact_id = ? AND viewer_id = ? ORDER BY position LIMIT 1",
|
|
224
|
+
artifactId,
|
|
225
|
+
viewerId,
|
|
226
|
+
)
|
|
227
|
+
: db.get<SubmissionRow>(
|
|
228
|
+
"SELECT * FROM submission WHERE artifact_id = ? ORDER BY position LIMIT 1",
|
|
229
|
+
artifactId,
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Refuses a write to a draft that a `once` submission closed (D106). */
|
|
234
|
+
export function assertDraftOpen(db: LedgerDb, artifactId: string, viewerId: string): void {
|
|
235
|
+
const closing = closingSubmission(db, artifactId, viewerId);
|
|
236
|
+
if (closing) {
|
|
237
|
+
throw new SubmitConflict(
|
|
238
|
+
`A submission made at ${closing.created_at} closed this draft: the artifact takes one submission.`,
|
|
239
|
+
submissionSummary(db, closing),
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function snapshotFields(rows: readonly FieldRow[]): Record<string, SnapshotField> {
|
|
245
|
+
// fromEntries defines own properties, so a field ID such as `__proto__` stays a field.
|
|
246
|
+
return Object.fromEntries(
|
|
247
|
+
rows.map((row) => [
|
|
248
|
+
row.field_id,
|
|
249
|
+
{
|
|
250
|
+
value: JSON.parse(row.value_json) as unknown,
|
|
251
|
+
...(row.label !== null ? { label: row.label } : {}),
|
|
252
|
+
...(row.required !== null ? { required: row.required === 1 } : {}),
|
|
253
|
+
...(row.sensitive === 1 ? { sensitive: true } : {}),
|
|
254
|
+
},
|
|
255
|
+
]),
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Refuses a submit through a channel the view doesn't accept (D40, D108):
|
|
261
|
+
* `MOSTRA_SUBMIT_CHANNEL_DENIED`.
|
|
262
|
+
*/
|
|
263
|
+
export function assertChannel(view: ViewRow, channel: SubmitChannel): void {
|
|
264
|
+
if (viewControls(view).submitChannels.includes(channel)) return;
|
|
265
|
+
throw new RpcError(
|
|
266
|
+
createDiagnostic("MOSTRA_SUBMIT_CHANNEL_DENIED", {
|
|
267
|
+
message:
|
|
268
|
+
channel === "artifact"
|
|
269
|
+
? "This view takes submissions and choices from the shell's trusted controls only."
|
|
270
|
+
: "This view takes submissions and choices from the artifact's own code only.",
|
|
271
|
+
}),
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Draft fields a view's version doesn't cover, as results deliver them (D42, D109). A field
|
|
277
|
+
* records the version whose view last wrote it; one written before Mostra kept that (an
|
|
278
|
+
* unreleased ledger of WP-06's time, which migration 6 fills from its events where it can) is
|
|
279
|
+
* attributed to version 1, the earliest it can have come from.
|
|
280
|
+
*/
|
|
281
|
+
export function orphansOf(rows: readonly FieldRow[]): OrphanedField[] {
|
|
282
|
+
return rows.map((row) => ({
|
|
283
|
+
id: row.field_id,
|
|
284
|
+
...(row.label !== null ? { label: row.label } : {}),
|
|
285
|
+
value: JSON.parse(row.value_json) as unknown,
|
|
286
|
+
fromVersion: row.version_number ?? 1,
|
|
287
|
+
}));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** What a submit names. */
|
|
291
|
+
export interface SubmitInput {
|
|
292
|
+
readonly artifactId: string;
|
|
293
|
+
/** The viewer whose session submits. */
|
|
294
|
+
readonly viewerId: string;
|
|
295
|
+
/** The link the page's session came from: the view must be one it opened. */
|
|
296
|
+
readonly tokenId?: string;
|
|
297
|
+
/** The view the page was opened as; its version is what the submission names (D104). */
|
|
298
|
+
readonly viewId: string;
|
|
299
|
+
/** The draft revision the viewer saw. */
|
|
300
|
+
readonly expectedRevision: number;
|
|
301
|
+
readonly channel: SubmitChannel;
|
|
302
|
+
/** The hosts to deliver to (D105): registered or expected now; empty for the built-in adapter. */
|
|
303
|
+
readonly owners: readonly string[];
|
|
304
|
+
/** Whether the view's version covers a field ID (D109); the others are the orphans. */
|
|
305
|
+
readonly covers: (versionId: string) => (fieldId: string) => boolean;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** What a submit did. */
|
|
309
|
+
export interface SubmitOutcome {
|
|
310
|
+
readonly submission: SubmissionSummary;
|
|
311
|
+
/** `false` when it returned an earlier submission of the same view and revision (D22). */
|
|
312
|
+
readonly created: boolean;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Submits a viewer's draft, in the caller's write transaction (ARCHITECTURE §6). Refusals:
|
|
317
|
+
* `MOSTRA_SUBMIT_VIEW_INVALID` for a view of another viewer or artifact,
|
|
318
|
+
* `MOSTRA_SUBMIT_POLICY_NONE`, `MOSTRA_SUBMIT_ALREADY` (a {@link SubmitConflict}) under `once`,
|
|
319
|
+
* `MOSTRA_SUBMIT_REVISION_MOVED` when the draft changed since the viewer saw it.
|
|
320
|
+
*/
|
|
321
|
+
export function submit(db: LedgerDb, input: SubmitInput): SubmitOutcome {
|
|
322
|
+
// 1. The view, and through it the exact version the viewer saw, and the channels it accepts.
|
|
323
|
+
const view = viewFor(db, input.viewId, input.artifactId, input.viewerId, input.tokenId);
|
|
324
|
+
assertChannel(view, input.channel);
|
|
325
|
+
const draft = documentFor(db, input.artifactId, input.viewerId, true);
|
|
326
|
+
// A repeat of the same submit (double click, retry, a second request in flight) gets the
|
|
327
|
+
// submission it made (D22), whatever has happened since.
|
|
328
|
+
const repeat = db.get<SubmissionRow>(
|
|
329
|
+
"SELECT * FROM submission WHERE view_id = ? AND draft_document_id = ? AND draft_revision = ? ORDER BY position DESC LIMIT 1",
|
|
330
|
+
view.id,
|
|
331
|
+
draft.id,
|
|
332
|
+
input.expectedRevision,
|
|
333
|
+
);
|
|
334
|
+
if (repeat)
|
|
335
|
+
return { submission: { ...submissionSummary(db, repeat), duplicate: true }, created: false };
|
|
336
|
+
|
|
337
|
+
// 2. The policy in force.
|
|
338
|
+
const artifact = db.get<{ policy_json: string | null; latest_version_id: string | null }>(
|
|
339
|
+
"SELECT policy_json, latest_version_id FROM artifact WHERE id = ?",
|
|
340
|
+
input.artifactId,
|
|
341
|
+
);
|
|
342
|
+
if (!artifact) throw new Error(`No artifact ${input.artifactId}.`);
|
|
343
|
+
const policy = storedPolicy(artifact.policy_json);
|
|
344
|
+
if (policy.policy === "none") {
|
|
345
|
+
throw new RpcError(
|
|
346
|
+
createDiagnostic("MOSTRA_SUBMIT_POLICY_NONE", {
|
|
347
|
+
message: "This artifact's submission policy is none: it takes no submissions.",
|
|
348
|
+
}),
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
const closing = closingSubmission(db, input.artifactId, input.viewerId);
|
|
352
|
+
if (closing) {
|
|
353
|
+
throw new SubmitConflict(
|
|
354
|
+
`This artifact takes one submission ${policy.policy === "once" && policy.scope === "per-viewer" ? "per viewer" : "in all"}, and it was made at ${closing.created_at}.`,
|
|
355
|
+
submissionSummary(db, closing),
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// 3. The draft revision the viewer saw.
|
|
360
|
+
if (draft.revision !== input.expectedRevision) {
|
|
361
|
+
throw new RpcError(
|
|
362
|
+
createDiagnostic("MOSTRA_SUBMIT_REVISION_MOVED", {
|
|
363
|
+
message: `The draft is at revision ${draft.revision}, not ${input.expectedRevision}: it changed after the viewer saw it.`,
|
|
364
|
+
}),
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// 4. The canonical persisted draft and the artifact state at its current revision.
|
|
369
|
+
const draftRows = db.all<FieldRow>(
|
|
370
|
+
"SELECT * FROM field WHERE document_id = ? AND deleted = 0 ORDER BY field_id",
|
|
371
|
+
draft.id,
|
|
372
|
+
);
|
|
373
|
+
const state = documentFor(db, input.artifactId, null);
|
|
374
|
+
const stateRows = state
|
|
375
|
+
? db.all<FieldRow>(
|
|
376
|
+
"SELECT * FROM field WHERE document_id = ? AND deleted = 0 ORDER BY field_id",
|
|
377
|
+
state.id,
|
|
378
|
+
)
|
|
379
|
+
: [];
|
|
380
|
+
const version = db.get<{ number: number }>(
|
|
381
|
+
"SELECT number FROM version WHERE id = ?",
|
|
382
|
+
view.version_id,
|
|
383
|
+
) as { number: number };
|
|
384
|
+
const covered = input.covers(view.version_id);
|
|
385
|
+
const latest =
|
|
386
|
+
artifact.latest_version_id === null
|
|
387
|
+
? undefined
|
|
388
|
+
: db.get<{ id: string; number: number }>(
|
|
389
|
+
"SELECT id, number FROM version WHERE id = ?",
|
|
390
|
+
artifact.latest_version_id,
|
|
391
|
+
);
|
|
392
|
+
const staleView = latest !== undefined && latest.id !== view.version_id;
|
|
393
|
+
|
|
394
|
+
// Epochs (D103): the viewer's next round; once: the one submission of its scope.
|
|
395
|
+
const scopeKey =
|
|
396
|
+
policy.policy === "once" && policy.scope === "per-artifact" ? "" : input.viewerId;
|
|
397
|
+
const previous =
|
|
398
|
+
policy.policy === "epochs"
|
|
399
|
+
? db.get<{ id: string; epoch: number }>(
|
|
400
|
+
"SELECT id, epoch FROM submission WHERE artifact_id = ? AND viewer_id = ? ORDER BY position DESC LIMIT 1",
|
|
401
|
+
input.artifactId,
|
|
402
|
+
input.viewerId,
|
|
403
|
+
)
|
|
404
|
+
: undefined;
|
|
405
|
+
const epoch =
|
|
406
|
+
policy.policy === "epochs"
|
|
407
|
+
? Math.max(
|
|
408
|
+
draft.epoch,
|
|
409
|
+
(db.get<{ epoch: number | null }>(
|
|
410
|
+
"SELECT MAX(epoch) AS epoch FROM submission WHERE artifact_id = ? AND scope_key = ?",
|
|
411
|
+
input.artifactId,
|
|
412
|
+
scopeKey,
|
|
413
|
+
)?.epoch ?? 0) + 1,
|
|
414
|
+
)
|
|
415
|
+
: 1;
|
|
416
|
+
|
|
417
|
+
const submissionId = SubmissionId.create();
|
|
418
|
+
const at = new Date().toISOString();
|
|
419
|
+
const viewerRecord = viewerById(db, input.viewerId);
|
|
420
|
+
// The view's signals, newest first up to the limits, so a snapshot stays bounded however many
|
|
421
|
+
// a page raised (Claude review finding 2); the count left out is recorded.
|
|
422
|
+
const signalRows = db.all<{ at: string; payload_json: string }>(
|
|
423
|
+
"SELECT at, payload_json FROM event WHERE artifact_id = ? AND type = 'signal.raised' AND json_extract(payload_json, '$.viewId') = ? AND json_extract(payload_json, '$.name') != ? ORDER BY cursor DESC LIMIT ?",
|
|
424
|
+
input.artifactId,
|
|
425
|
+
view.id,
|
|
426
|
+
SUBMIT_SIGNAL,
|
|
427
|
+
SNAPSHOT_SIGNALS_MAX,
|
|
428
|
+
);
|
|
429
|
+
const signals: { name: string; at: string; payload?: unknown }[] = [];
|
|
430
|
+
let signalBytes = 0;
|
|
431
|
+
for (const row of signalRows) {
|
|
432
|
+
signalBytes += row.payload_json.length;
|
|
433
|
+
if (signalBytes > SNAPSHOT_SIGNAL_BYTES_MAX) break;
|
|
434
|
+
const payload = JSON.parse(row.payload_json) as { name: string; payload?: unknown };
|
|
435
|
+
signals.unshift({
|
|
436
|
+
name: payload.name,
|
|
437
|
+
at: row.at,
|
|
438
|
+
...(payload.payload !== undefined ? { payload: payload.payload } : {}),
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
const signalCount =
|
|
442
|
+
db.get<{ n: number }>(
|
|
443
|
+
"SELECT COUNT(*) AS n FROM event WHERE artifact_id = ? AND type = 'signal.raised' AND json_extract(payload_json, '$.viewId') = ? AND json_extract(payload_json, '$.name') != ?",
|
|
444
|
+
input.artifactId,
|
|
445
|
+
view.id,
|
|
446
|
+
SUBMIT_SIGNAL,
|
|
447
|
+
)?.n ?? 0;
|
|
448
|
+
const signalsOmitted = signalCount - signals.length;
|
|
449
|
+
// Compaction may have removed some of the view's signals; the snapshot says so (finding 10).
|
|
450
|
+
const signalsCompacted =
|
|
451
|
+
view.opened_cursor !== null &&
|
|
452
|
+
db.get(
|
|
453
|
+
"SELECT 1 FROM event WHERE artifact_id = ? AND type = 'artifact.compacted' AND cursor >= ? AND json_extract(payload_json, '$.types.\"signal.raised\"') > 0",
|
|
454
|
+
input.artifactId,
|
|
455
|
+
view.opened_cursor,
|
|
456
|
+
) !== undefined;
|
|
457
|
+
// The view's action runs, as they stood when the viewer submitted (D101: results list them).
|
|
458
|
+
const actions = db
|
|
459
|
+
.all<{ id: string; action: string; status: string; started_at: string }>(
|
|
460
|
+
"SELECT id, action, status, started_at FROM action_run WHERE view_id = ? ORDER BY started_at, id LIMIT ?",
|
|
461
|
+
view.id,
|
|
462
|
+
SNAPSHOT_ACTIONS_MAX,
|
|
463
|
+
)
|
|
464
|
+
.map((run) => ({ runId: run.id, action: run.action, status: run.status, at: run.started_at }));
|
|
465
|
+
const snapshot: Snapshot = {
|
|
466
|
+
schema: 1,
|
|
467
|
+
submissionId,
|
|
468
|
+
at,
|
|
469
|
+
artifactId: input.artifactId,
|
|
470
|
+
versionId: view.version_id,
|
|
471
|
+
version: version.number,
|
|
472
|
+
viewId: view.id,
|
|
473
|
+
viewer: viewerRecord,
|
|
474
|
+
channel: input.channel,
|
|
475
|
+
policy,
|
|
476
|
+
epoch,
|
|
477
|
+
...(previous ? { previous: previous.id } : {}),
|
|
478
|
+
staleView,
|
|
479
|
+
...(staleView && latest ? { latestVersion: latest.number } : {}),
|
|
480
|
+
draft: {
|
|
481
|
+
revision: String(draft.revision),
|
|
482
|
+
fields: snapshotFields(draftRows.filter((row) => covered(row.field_id))),
|
|
483
|
+
},
|
|
484
|
+
...(state
|
|
485
|
+
? { state: { revision: String(state.revision), fields: snapshotFields(stateRows) } }
|
|
486
|
+
: {}),
|
|
487
|
+
// Fields the view's version doesn't cover are delivered too, labelled (D42, D109).
|
|
488
|
+
orphaned: orphansOf(draftRows.filter((row) => !covered(row.field_id))),
|
|
489
|
+
signals,
|
|
490
|
+
...(signalsOmitted > 0 ? { signalsOmitted } : {}),
|
|
491
|
+
...(signalsCompacted ? { signalsCompacted: true as const } : {}),
|
|
492
|
+
actions,
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
// 5. The digest of the canonical snapshot, which must fit one protocol message with room to
|
|
496
|
+
// spare, or no host could ever receive it.
|
|
497
|
+
const snapshotJson = canonicalJson(snapshot);
|
|
498
|
+
if (snapshotJson.length > SNAPSHOT_MAX_LENGTH) {
|
|
499
|
+
throw new RpcError(
|
|
500
|
+
createDiagnostic("MOSTRA_FIELD_TOO_LARGE", {
|
|
501
|
+
message: `This submission would be ${snapshotJson.length} characters, more than a submission may hold (${SNAPSHOT_MAX_LENGTH}).`,
|
|
502
|
+
}),
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
const digest = createHash("sha256").update(snapshotJson, "utf8").digest("hex");
|
|
506
|
+
|
|
507
|
+
// 8 (first, for its cursor). `submission.created`; its cursor is the submission's position,
|
|
508
|
+
// the order deliveries replay in.
|
|
509
|
+
const actor = { kind: "viewer" as const, id: input.viewerId };
|
|
510
|
+
const causation = { submissionId };
|
|
511
|
+
const position = Number(
|
|
512
|
+
appendEvent(db, {
|
|
513
|
+
artifactId: input.artifactId,
|
|
514
|
+
type: "submission.created",
|
|
515
|
+
actor,
|
|
516
|
+
causation,
|
|
517
|
+
payload: {
|
|
518
|
+
submissionId,
|
|
519
|
+
versionId: view.version_id,
|
|
520
|
+
version: version.number,
|
|
521
|
+
viewId: view.id,
|
|
522
|
+
viewerId: input.viewerId,
|
|
523
|
+
channel: input.channel,
|
|
524
|
+
epoch,
|
|
525
|
+
staleView,
|
|
526
|
+
digest,
|
|
527
|
+
},
|
|
528
|
+
}),
|
|
529
|
+
);
|
|
530
|
+
// 6. The immutable submission.
|
|
531
|
+
db.run(
|
|
532
|
+
`INSERT INTO submission (id, position, artifact_id, version_id, view_id, viewer_id, channel, policy_json, epoch, scope_key,
|
|
533
|
+
previous_id, draft_document_id, draft_revision, state_revision, latest_version_number, snapshot_json, digest, stale_view, request_id, created_at)
|
|
534
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?)`,
|
|
535
|
+
submissionId,
|
|
536
|
+
position,
|
|
537
|
+
input.artifactId,
|
|
538
|
+
view.version_id,
|
|
539
|
+
view.id,
|
|
540
|
+
input.viewerId,
|
|
541
|
+
input.channel,
|
|
542
|
+
JSON.stringify(policy),
|
|
543
|
+
epoch,
|
|
544
|
+
scopeKey,
|
|
545
|
+
previous?.id ?? null,
|
|
546
|
+
draft.id,
|
|
547
|
+
draft.revision,
|
|
548
|
+
state?.revision ?? null,
|
|
549
|
+
staleView && latest ? latest.number : null,
|
|
550
|
+
snapshotJson,
|
|
551
|
+
digest,
|
|
552
|
+
staleView ? 1 : 0,
|
|
553
|
+
at,
|
|
554
|
+
);
|
|
555
|
+
// The next round starts with the same values (D103).
|
|
556
|
+
if (policy.policy === "epochs") {
|
|
557
|
+
db.run("UPDATE document SET epoch = ? WHERE id = ?", epoch + 1, draft.id);
|
|
558
|
+
}
|
|
559
|
+
// 7. One delivery per host, durable with the submission (IN-12).
|
|
560
|
+
const owners = input.owners.length > 0 ? [...new Set(input.owners)].sort() : [BUILT_IN_OWNER];
|
|
561
|
+
for (const owner of owners) {
|
|
562
|
+
db.run(
|
|
563
|
+
"INSERT INTO delivery (id, submission_id, position, port_owner, state, attempts, next_attempt_at, updated_at) VALUES (?, ?, ?, ?, 'pending', 0, ?, ?)",
|
|
564
|
+
DeliveryId.create(),
|
|
565
|
+
submissionId,
|
|
566
|
+
position,
|
|
567
|
+
owner,
|
|
568
|
+
at,
|
|
569
|
+
at,
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
// Submit is the standard signal (D106).
|
|
573
|
+
raiseSubmitSignal(db, {
|
|
574
|
+
artifactId: input.artifactId,
|
|
575
|
+
submissionId,
|
|
576
|
+
channel: input.channel,
|
|
577
|
+
actor,
|
|
578
|
+
viewId: view.id,
|
|
579
|
+
});
|
|
580
|
+
const row = db.get<SubmissionRow>(
|
|
581
|
+
"SELECT * FROM submission WHERE id = ?",
|
|
582
|
+
submissionId,
|
|
583
|
+
) as SubmissionRow;
|
|
584
|
+
return { submission: submissionSummary(db, row), created: true };
|
|
585
|
+
}
|