@mostra/protocol 0.10.0 → 0.11.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/browser.d.ts +42 -0
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +29 -1
- package/dist/browser.js.map +1 -1
- package/dist/diagnostics/codes/action.d.ts +1 -1
- package/dist/diagnostics/codes/action.d.ts.map +1 -1
- package/dist/diagnostics/codes/action.js +12 -1
- package/dist/diagnostics/codes/action.js.map +1 -1
- package/dist/diagnostics/codes/remote.d.ts +1 -1
- package/dist/diagnostics/codes/remote.d.ts.map +1 -1
- package/dist/diagnostics/codes/remote.js +78 -1
- package/dist/diagnostics/codes/remote.js.map +1 -1
- package/dist/diagnostics/codes/submit.d.ts +1 -1
- package/dist/diagnostics/codes/submit.d.ts.map +1 -1
- package/dist/diagnostics/codes/submit.js +12 -1
- package/dist/diagnostics/codes/submit.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -1
- package/dist/interaction.d.ts +5 -0
- package/dist/interaction.d.ts.map +1 -1
- package/dist/interaction.js +2 -1
- package/dist/interaction.js.map +1 -1
- package/dist/methods/artifact/pause.d.ts +33 -0
- package/dist/methods/artifact/pause.d.ts.map +1 -0
- package/dist/methods/artifact/pause.js +33 -0
- package/dist/methods/artifact/pause.js.map +1 -0
- package/dist/methods/index.d.ts.map +1 -1
- package/dist/methods/index.js +7 -1
- package/dist/methods/index.js.map +1 -1
- package/dist/methods/port/relay-author.d.ts +28 -0
- package/dist/methods/port/relay-author.d.ts.map +1 -0
- package/dist/methods/port/relay-author.js +42 -0
- package/dist/methods/port/relay-author.js.map +1 -0
- package/dist/methods/remote/link.d.ts +40 -0
- package/dist/methods/remote/link.d.ts.map +1 -0
- package/dist/methods/remote/link.js +40 -0
- package/dist/methods/remote/link.js.map +1 -0
- package/dist/operator.d.ts +464 -0
- package/dist/operator.d.ts.map +1 -0
- package/dist/operator.js +134 -0
- package/dist/operator.js.map +1 -0
- package/package.json +11 -1
- package/schemas/protocol.draft-07.json +259 -0
- package/schemas/protocol.schema.json +259 -0
- package/src/browser.ts +64 -0
- package/src/diagnostics/codes/action.ts +12 -0
- package/src/diagnostics/codes/remote.ts +84 -0
- package/src/diagnostics/codes/submit.ts +12 -0
- package/src/index.ts +19 -0
- package/src/interaction.ts +6 -0
- package/src/methods/artifact/pause.ts +59 -0
- package/src/methods/index.ts +6 -0
- package/src/methods/port/relay-author.ts +57 -0
- package/src/methods/remote/link.ts +65 -0
- package/src/operator.ts +621 -0
package/src/operator.ts
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The operator boundary of a remote server (ARCHITECTURE §15.10, INTERFACES §18.6, D165 items 9
|
|
3
|
+
* and 11): the ports an operating layer implements in an ES module the server loads in process,
|
|
4
|
+
* and the handle the server gives it. Each port is optional and has a no-op default, so a
|
|
5
|
+
* self-hoster needs none of it; each call is async and time-limited, and the server checks every
|
|
6
|
+
* answer against the schemas below before it acts on one (an answer they refuse counts as no
|
|
7
|
+
* answer).
|
|
8
|
+
*
|
|
9
|
+
* An operating layer imports nothing of Mostra but this module (`@mostra/protocol/operator`), so
|
|
10
|
+
* the surface it builds on is this file's, versioned with `@mostra/protocol` and changed only
|
|
11
|
+
* additively (D32).
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { defineOperator } from "@mostra/protocol/operator";
|
|
15
|
+
*
|
|
16
|
+
* export default defineOperator((server) => ({
|
|
17
|
+
* limits: { check: async (q) => (q.operation === "share" ? { allowed: true } : { allowed: true }) },
|
|
18
|
+
* abuse: { report: async (r) => { await server.quarantine({ routeId: r.routeId }, r.reason); } },
|
|
19
|
+
* }));
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Every port call and every handle target names a hosted route (`rte_…`): an artifact ID, and a
|
|
23
|
+
* version ID derived from it (D89), repeat across homes when a project is copied, so a bare ID
|
|
24
|
+
* would reach every tenant hosting it.
|
|
25
|
+
*/
|
|
26
|
+
import * as z from "zod";
|
|
27
|
+
|
|
28
|
+
/** The operator boundary's own revision, which a module may check (additive within it). */
|
|
29
|
+
export const OPERATOR_REVISION = "2026-11-08";
|
|
30
|
+
|
|
31
|
+
/** How long the server waits for each port, in milliseconds, before it acts as if none answered. */
|
|
32
|
+
export const OPERATOR_TIME_LIMITS: {
|
|
33
|
+
readonly limits: number;
|
|
34
|
+
readonly policy: number;
|
|
35
|
+
readonly routing: number;
|
|
36
|
+
readonly identity: number;
|
|
37
|
+
readonly accounts: number;
|
|
38
|
+
readonly authenticate: number;
|
|
39
|
+
readonly pushed: number;
|
|
40
|
+
readonly outbound: number;
|
|
41
|
+
} = {
|
|
42
|
+
limits: 2_000,
|
|
43
|
+
policy: 2_000,
|
|
44
|
+
routing: 2_000,
|
|
45
|
+
identity: 2_000,
|
|
46
|
+
accounts: 5_000,
|
|
47
|
+
authenticate: 5_000,
|
|
48
|
+
pushed: 30_000,
|
|
49
|
+
outbound: 30_000,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// ── limits ──────────────────────────────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* An operation `limits.check` judges. When the operator doesn't answer in time, `share`,
|
|
56
|
+
* `version.push`, `link` and authoring fail closed, and `view`, `submit` and draft writes proceed,
|
|
57
|
+
* so nothing a person typed is lost (D165 item 9).
|
|
58
|
+
*/
|
|
59
|
+
export type LimitOperation =
|
|
60
|
+
| "share"
|
|
61
|
+
| "version.push"
|
|
62
|
+
| "link"
|
|
63
|
+
| "view"
|
|
64
|
+
| "submit"
|
|
65
|
+
| "run"
|
|
66
|
+
| "run.wait"
|
|
67
|
+
| "call"
|
|
68
|
+
| "mcp.call"
|
|
69
|
+
| "storage";
|
|
70
|
+
|
|
71
|
+
/** What `limits.check` is asked. */
|
|
72
|
+
export interface LimitQuery {
|
|
73
|
+
/** The account the operation counts against. */
|
|
74
|
+
readonly account: string;
|
|
75
|
+
/** The account's hosted home. */
|
|
76
|
+
readonly home: string;
|
|
77
|
+
/** The artifact, for operations on one. */
|
|
78
|
+
readonly artifact?: string;
|
|
79
|
+
/** The artifact's hosted route, for operations on one (WP-30). */
|
|
80
|
+
readonly routeId?: string;
|
|
81
|
+
/** What is asked for. */
|
|
82
|
+
readonly operation: LimitOperation;
|
|
83
|
+
/** How much: bytes for `storage` and `version.push`, else 1. */
|
|
84
|
+
readonly quantity?: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** How long the server may keep an answer instead of asking again, in milliseconds (WP-30). */
|
|
88
|
+
export interface Cacheable {
|
|
89
|
+
/** Absent or 0: ask again next time. */
|
|
90
|
+
readonly cacheForMs?: number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** `limits.check`'s answer. */
|
|
94
|
+
export type LimitAnswer =
|
|
95
|
+
| ({ readonly allowed: true } & Cacheable)
|
|
96
|
+
| ({
|
|
97
|
+
readonly allowed: false;
|
|
98
|
+
/** A diagnostic code of the operator's (the server answers `MOSTRA_REMOTE_LIMIT_REACHED`). */
|
|
99
|
+
readonly code: string;
|
|
100
|
+
/** Shown as text to whoever was refused. */
|
|
101
|
+
readonly message: string;
|
|
102
|
+
readonly retryAfterMs?: number;
|
|
103
|
+
} & Cacheable);
|
|
104
|
+
|
|
105
|
+
const cacheFor = z.int().min(0).max(86_400_000).exactOptional();
|
|
106
|
+
|
|
107
|
+
/** The schema the server checks `limits.check`'s answer against. */
|
|
108
|
+
export const LimitAnswer: z.ZodType<LimitAnswer> = z.union([
|
|
109
|
+
z.object({ allowed: z.literal(true), cacheForMs: cacheFor }),
|
|
110
|
+
z.object({
|
|
111
|
+
allowed: z.literal(false),
|
|
112
|
+
code: z.string().min(1).max(128),
|
|
113
|
+
message: z.string().max(1_000),
|
|
114
|
+
retryAfterMs: z.int().min(0).max(86_400_000).exactOptional(),
|
|
115
|
+
cacheForMs: cacheFor,
|
|
116
|
+
}),
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
// ── metering ────────────────────────────────────────────────────────────────────────────
|
|
120
|
+
|
|
121
|
+
/** A counter's name (INTERFACES §18.6). */
|
|
122
|
+
export type MeterName =
|
|
123
|
+
| "views"
|
|
124
|
+
| "submissions"
|
|
125
|
+
| "requests"
|
|
126
|
+
| "bytes.stored"
|
|
127
|
+
| "bytes.egress"
|
|
128
|
+
| "relay.messages"
|
|
129
|
+
| "mcp.calls"
|
|
130
|
+
| "home.activeSeconds"
|
|
131
|
+
| (string & {});
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* One counter of a batch. `views` carry `viewerKey`, a monthly pseudonymous key (an HMAC under a
|
|
135
|
+
* key the server rotates each month), never an identity: per account for a signed-in viewer, per
|
|
136
|
+
* viewer for a link minted with one, and per route for an anonymous viewer, since the shell gives
|
|
137
|
+
* each route a different key and none spans routes (HY-08). `mcp.calls` carry `client`, the MCP
|
|
138
|
+
* client's `clientInfo` name (D148).
|
|
139
|
+
*/
|
|
140
|
+
export interface MeterCounter {
|
|
141
|
+
/** What is counted. */
|
|
142
|
+
readonly name: MeterName;
|
|
143
|
+
/** The account it counts against. */
|
|
144
|
+
readonly account: string;
|
|
145
|
+
/** The account's hosted home. */
|
|
146
|
+
readonly home?: string;
|
|
147
|
+
/** The artifact, for counters of one. */
|
|
148
|
+
readonly artifactId?: string;
|
|
149
|
+
/** The artifact's hosted route, for counters of one. */
|
|
150
|
+
readonly routeId?: string;
|
|
151
|
+
/** How much in the batch's window: a count, bytes or seconds. */
|
|
152
|
+
readonly value: number;
|
|
153
|
+
/** What tells counters of one name apart (`viewerKey`, `client`). */
|
|
154
|
+
readonly dimensions?: Readonly<Record<string, string>>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** What `metering.record` receives, at least once, idempotent by `batchId`. */
|
|
158
|
+
export interface MeterBatch {
|
|
159
|
+
/** Stable across retries: a batch delivered twice is the same batch. */
|
|
160
|
+
readonly batchId: string;
|
|
161
|
+
/** Where the window the counters cover starts. */
|
|
162
|
+
readonly from: string;
|
|
163
|
+
/** Where it ends. */
|
|
164
|
+
readonly to: string;
|
|
165
|
+
/** The counters, one per name, account, route and dimensions. */
|
|
166
|
+
readonly counters: readonly MeterCounter[];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ── sign-in: identity providers and accounts ────────────────────────────────────────────
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Who an identity provider says signed in (INTERFACES §18.5). `facts` carries what a provider
|
|
173
|
+
* tells beyond that (GitHub's account age, `createdAt`), for an operator's policy.
|
|
174
|
+
*/
|
|
175
|
+
export interface SignInIdentity {
|
|
176
|
+
/** The provider's ID (`IdentityProvider.id`). */
|
|
177
|
+
readonly provider: string;
|
|
178
|
+
/** The provider's own stable ID for the person. */
|
|
179
|
+
readonly subject: string;
|
|
180
|
+
/** The person's email address, when the provider gives one. */
|
|
181
|
+
readonly email?: string;
|
|
182
|
+
/** Whether the provider verified `email`. */
|
|
183
|
+
readonly emailVerified?: boolean;
|
|
184
|
+
/** The display name the provider gives. */
|
|
185
|
+
readonly name?: string;
|
|
186
|
+
/** What else the provider tells (an account's age as `createdAt`). */
|
|
187
|
+
readonly facts?: Readonly<Record<string, unknown>>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** The schema the server checks a provider's `SignInIdentity` against. */
|
|
191
|
+
export const SignInIdentity: z.ZodType<SignInIdentity> = z.object({
|
|
192
|
+
provider: z.string().regex(/^[a-z][a-z0-9-]{0,31}$/),
|
|
193
|
+
subject: z.string().min(1).max(256),
|
|
194
|
+
email: z.string().max(320).exactOptional(),
|
|
195
|
+
emailVerified: z.boolean().exactOptional(),
|
|
196
|
+
name: z.string().max(256).exactOptional(),
|
|
197
|
+
facts: z.record(z.string(), z.unknown()).exactOptional(),
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
/** PKCE for one sign-in at a provider: the server makes it; the provider sends `challenge`. */
|
|
201
|
+
export interface SignInPkce {
|
|
202
|
+
/** The S256 challenge the provider's authorization request carries. */
|
|
203
|
+
readonly challenge: string;
|
|
204
|
+
/** Always S256. */
|
|
205
|
+
readonly method: "S256";
|
|
206
|
+
/** The verifier the provider's code exchange sends. */
|
|
207
|
+
readonly verifier: string;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** What a provider's `authorize` and `callback` are told about the sign-in in progress. */
|
|
211
|
+
export interface SignInContext {
|
|
212
|
+
/** The provider's return address on the API origin: `<apiOrigin>/signin/<id>/callback`. */
|
|
213
|
+
readonly redirectUri: string;
|
|
214
|
+
/** A fresh value an OpenID Connect provider binds its ID token to. */
|
|
215
|
+
readonly nonce: string;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* An identity provider behind the sign-in port (D165 item 8): the built-in OpenID Connect and
|
|
220
|
+
* GitHub adapters, a development one for tests, or one an operator module supplies.
|
|
221
|
+
*/
|
|
222
|
+
export interface IdentityProvider {
|
|
223
|
+
/** Lower-case letters, digits and hyphens, at most 32: part of its callback's path. */
|
|
224
|
+
readonly id: string;
|
|
225
|
+
/** What the sign-in page calls it ("GitHub"). */
|
|
226
|
+
readonly label: string;
|
|
227
|
+
/** Where to send the person: the provider's authorization URL with `state` and PKCE. */
|
|
228
|
+
authorize(state: string, pkce: SignInPkce, context: SignInContext): string | Promise<string>;
|
|
229
|
+
/**
|
|
230
|
+
* The person the provider names on return, from its callback's query (each parameter once; a
|
|
231
|
+
* repeated one is refused before this is called). The server has already
|
|
232
|
+
* checked `state`; the provider exchanges the code (with `pkce.verifier`) and checks what it
|
|
233
|
+
* gets. Throws when sign-in failed.
|
|
234
|
+
*/
|
|
235
|
+
callback(
|
|
236
|
+
params: Readonly<Record<string, string>>,
|
|
237
|
+
context: SignInContext & { readonly state: string; readonly pkce: SignInPkce },
|
|
238
|
+
): Promise<SignInIdentity>;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* `accounts.resolve`'s answer: the account (`status` `suspended` refuses sign-in), a **first
|
|
243
|
+
* sign-in step** on the operator's origin, or a refusal shown as text.
|
|
244
|
+
*
|
|
245
|
+
* A step: the server sends the person to `step.url` with `mostra_return` added, an address on the
|
|
246
|
+
* API origin (`/signin/return?step=<handle>`). The handle is single-use, lasts 15 minutes and is
|
|
247
|
+
* bound to the browser that started the sign-in (a `__Host-` cookie on the API origin, never on a
|
|
248
|
+
* user-content domain), so it can't be replayed or swapped. When the person comes back the server
|
|
249
|
+
* calls `resolve` again with the same identity and `context.steps` one higher; at most three steps
|
|
250
|
+
* per sign-in (WP-30, D167).
|
|
251
|
+
*/
|
|
252
|
+
export type AccountResolution =
|
|
253
|
+
| {
|
|
254
|
+
readonly account: string;
|
|
255
|
+
readonly name?: string;
|
|
256
|
+
readonly status: "active" | "suspended";
|
|
257
|
+
}
|
|
258
|
+
| { readonly step: { readonly url: string } }
|
|
259
|
+
| { readonly refused: { readonly message: string } };
|
|
260
|
+
|
|
261
|
+
/** The schema the server checks `accounts.resolve`'s answer against. */
|
|
262
|
+
export const AccountResolution: z.ZodType<AccountResolution> = z.union([
|
|
263
|
+
z.object({
|
|
264
|
+
account: z.string().min(1).max(128),
|
|
265
|
+
name: z.string().max(256).exactOptional(),
|
|
266
|
+
status: z.enum(["active", "suspended"]),
|
|
267
|
+
}),
|
|
268
|
+
z.object({ step: z.object({ url: z.url({ protocol: /^https$/ }).max(2_048) }) }),
|
|
269
|
+
z.object({ refused: z.object({ message: z.string().max(1_000) }) }),
|
|
270
|
+
]);
|
|
271
|
+
|
|
272
|
+
/** Where an operator-issued credential was presented (D165 item 9, Opus review of WP-28). */
|
|
273
|
+
export type CredentialResource = "host" | "mcp" | "files";
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* `accounts.authenticate`'s answer for a credential the operator issued, or `null`. `subject`
|
|
277
|
+
* names the credential stably (a service key's ID): a binding with it is one owner instance link
|
|
278
|
+
* of the account, kept per subject, so a program can run an always-on owner instance.
|
|
279
|
+
*/
|
|
280
|
+
export interface AuthenticatedCredential {
|
|
281
|
+
/** The account the credential acts for. */
|
|
282
|
+
readonly account: string;
|
|
283
|
+
/** What it may do: `mostra` for everything a sign-in may. */
|
|
284
|
+
readonly scope: readonly string[];
|
|
285
|
+
/** The credential's own stable name, which keys its instance link. */
|
|
286
|
+
readonly subject?: string;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** The schema the server checks `accounts.authenticate`'s answer against. */
|
|
290
|
+
export const AuthenticatedCredential: z.ZodType<AuthenticatedCredential | null> = z
|
|
291
|
+
.object({
|
|
292
|
+
account: z.string().min(1).max(128),
|
|
293
|
+
scope: z.array(z.string().max(64)).max(32),
|
|
294
|
+
subject: z
|
|
295
|
+
.string()
|
|
296
|
+
.regex(/^[A-Za-z0-9._:-]{1,128}$/)
|
|
297
|
+
.exactOptional(),
|
|
298
|
+
})
|
|
299
|
+
.nullable();
|
|
300
|
+
|
|
301
|
+
// ── abuse ───────────────────────────────────────────────────────────────────────────────
|
|
302
|
+
|
|
303
|
+
/** A report from the shell's report control (`POST /a/{routeId}/_mostra/report`). */
|
|
304
|
+
export interface AbuseReport {
|
|
305
|
+
/** The reported route. */
|
|
306
|
+
readonly routeId: string;
|
|
307
|
+
/** The route's label. */
|
|
308
|
+
readonly label: string;
|
|
309
|
+
/** The artifact's own ID (as its manifest names it). */
|
|
310
|
+
readonly artifactId: string;
|
|
311
|
+
/** The hosted home. */
|
|
312
|
+
readonly home: string;
|
|
313
|
+
/** The version the reporter saw, when the shell knew it. */
|
|
314
|
+
readonly versionId?: string;
|
|
315
|
+
/** The reason the reporter chose. */
|
|
316
|
+
readonly reason: string;
|
|
317
|
+
/** What the reporter wrote, as text. */
|
|
318
|
+
readonly detail?: string;
|
|
319
|
+
/** When the report was made. */
|
|
320
|
+
readonly at: string;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** What `abuse.pushed` receives after each version push, with read access to its client files. */
|
|
324
|
+
export interface PushedVersion {
|
|
325
|
+
/** The route the version was pushed to. */
|
|
326
|
+
readonly routeId: string;
|
|
327
|
+
/** The route's label. */
|
|
328
|
+
readonly label: string;
|
|
329
|
+
/** The artifact's own ID (as its manifest names it). */
|
|
330
|
+
readonly artifactId: string;
|
|
331
|
+
/** The hosted home. */
|
|
332
|
+
readonly home: string;
|
|
333
|
+
/** The pushed version. */
|
|
334
|
+
readonly versionId: string;
|
|
335
|
+
/** The version's `manifest.json`, parsed. */
|
|
336
|
+
readonly manifest: unknown;
|
|
337
|
+
/** Every client file's path (`client/…`). */
|
|
338
|
+
readonly files: readonly string[];
|
|
339
|
+
/** A client file's bytes, by its path in the listing. */
|
|
340
|
+
read(path: string): Promise<Uint8Array>;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** `abuse.pushed`'s answer: quarantine the version, or nothing. */
|
|
344
|
+
export type PushedAnswer = { readonly quarantine?: { readonly reason: string } } | undefined;
|
|
345
|
+
|
|
346
|
+
/** The schema the server checks `abuse.pushed`'s answer against. */
|
|
347
|
+
export const PushedAnswer: z.ZodType<PushedAnswer> = z
|
|
348
|
+
.object({ quarantine: z.object({ reason: z.string().max(1_000) }).exactOptional() })
|
|
349
|
+
.optional();
|
|
350
|
+
|
|
351
|
+
// ── policy ──────────────────────────────────────────────────────────────────────────────
|
|
352
|
+
|
|
353
|
+
/** What `policy.link` is asked about one link of one route. */
|
|
354
|
+
export interface LinkQuery {
|
|
355
|
+
/** The account that owns the route. */
|
|
356
|
+
readonly account: string;
|
|
357
|
+
/** Its hosted home. */
|
|
358
|
+
readonly home: string;
|
|
359
|
+
/** The route. */
|
|
360
|
+
readonly routeId: string;
|
|
361
|
+
/** The artifact's own ID (as its manifest names it). */
|
|
362
|
+
readonly artifactId: string;
|
|
363
|
+
/** The link, once it exists. */
|
|
364
|
+
readonly tokenId?: string;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Per link and artifact (D161): an interstitial before the page, the longest link lifetime and an
|
|
369
|
+
* idle expiry (durations such as `7d`), the after-submit line, the footer's wording (it can't be
|
|
370
|
+
* hidden on a public link), and the made-with mark.
|
|
371
|
+
*/
|
|
372
|
+
export interface LinkPolicy extends Cacheable {
|
|
373
|
+
/** A page the shell shows before the artifact, once per browser and link; its message as text. */
|
|
374
|
+
readonly interstitial?: { readonly kind: string; readonly message?: string };
|
|
375
|
+
/** The longest a link may live from when it was minted. */
|
|
376
|
+
readonly maxTtl?: string;
|
|
377
|
+
/** A link unused for this long expires. */
|
|
378
|
+
readonly idleExpiry?: string;
|
|
379
|
+
/** The line after "Your answer was sent.", as text. */
|
|
380
|
+
readonly afterSubmit?: string;
|
|
381
|
+
/** The footer's wording in place of "Never enter passwords", as text. */
|
|
382
|
+
readonly footer?: string;
|
|
383
|
+
/** Whether the made-with mark shows, and where it links. */
|
|
384
|
+
readonly madeWith?: { readonly show: boolean; readonly href?: string };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const duration = z.string().regex(/^[1-9][0-9]{0,8}[smhd]$/);
|
|
388
|
+
|
|
389
|
+
/** The schema the server checks `policy.link`'s answer against. */
|
|
390
|
+
export const LinkPolicy: z.ZodType<LinkPolicy> = z.object({
|
|
391
|
+
interstitial: z
|
|
392
|
+
.object({ kind: z.string().min(1).max(64), message: z.string().max(2_000).exactOptional() })
|
|
393
|
+
.exactOptional(),
|
|
394
|
+
maxTtl: duration.exactOptional(),
|
|
395
|
+
idleExpiry: duration.exactOptional(),
|
|
396
|
+
afterSubmit: z.string().max(500).exactOptional(),
|
|
397
|
+
footer: z.string().min(1).max(500).exactOptional(),
|
|
398
|
+
madeWith: z
|
|
399
|
+
.object({ show: z.boolean(), href: z.url({ protocol: /^https$/ }).exactOptional() })
|
|
400
|
+
.exactOptional(),
|
|
401
|
+
cacheForMs: cacheFor,
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* How Mostra behaves in one chat host (D160): how a page renders there (an MCP App resource, or
|
|
406
|
+
* the hosted link), and what a reached limit may say (`limit-reached` only, or the operator's
|
|
407
|
+
* message).
|
|
408
|
+
*/
|
|
409
|
+
export interface ChatHostPolicy {
|
|
410
|
+
/** `app`: through the MCP Apps resource; `link`: the hosted link only. */
|
|
411
|
+
readonly render?: "app" | "link";
|
|
412
|
+
/** What a reached limit may say there. */
|
|
413
|
+
readonly quota?: "limit-reached" | "message";
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** The schema of the `ChatHostPolicy` type, for configuration and `policy.chatHost`. */
|
|
417
|
+
export const ChatHostPolicy: z.ZodType<ChatHostPolicy> = z.object({
|
|
418
|
+
render: z.enum(["app", "link"]).exactOptional(),
|
|
419
|
+
quota: z.enum(["limit-reached", "message"]).exactOptional(),
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
// ── routing ─────────────────────────────────────────────────────────────────────────────
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* `routing.resolve`'s answer for an exact `Host` the server doesn't serve by itself (any host but
|
|
426
|
+
* a configured user-content domain's apex and its routes' labels):
|
|
427
|
+
*
|
|
428
|
+
* - `shell`: the host shows the hosted shell (`/a/<routeId>`) for one home's routes, whose frames
|
|
429
|
+
* are served on `<route label>.<domain>`. `domain` is a configured user-content domain, or one
|
|
430
|
+
* the operator supplies (an organization's own, whose TLS the operator's edge terminates for the
|
|
431
|
+
* host and every label under `domain`); the server serves those labels once the host is
|
|
432
|
+
* recorded, without asking again.
|
|
433
|
+
* - `route`: the host shows the shell of one route only (`/a/<routeId>`), with its frames on its
|
|
434
|
+
* label under the active domain: a chosen, readable name.
|
|
435
|
+
* - `null`: nothing is served (421).
|
|
436
|
+
*
|
|
437
|
+
* A host the operator supplies is recorded like the server's own labels, bound to its home
|
|
438
|
+
* forever: never issued to another home, so no origin's storage passes between tenants (D165).
|
|
439
|
+
*/
|
|
440
|
+
export type RoutingAnswer =
|
|
441
|
+
| ({ readonly shell: { readonly domain: string; readonly home: string } } & Cacheable)
|
|
442
|
+
| ({ readonly route: { readonly home: string; readonly routeId: string } } & Cacheable)
|
|
443
|
+
| null;
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* A host name of at least two labels, lower case; a port only as a local test server's hosts carry
|
|
447
|
+
* one (the server's own configuration allows the same).
|
|
448
|
+
*/
|
|
449
|
+
const hostName = z
|
|
450
|
+
.string()
|
|
451
|
+
.regex(
|
|
452
|
+
/^(?=.{1,253}(:|$))[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+(:\d{1,5})?$/,
|
|
453
|
+
);
|
|
454
|
+
|
|
455
|
+
/** The schema the server checks `routing.resolve`'s answer against. */
|
|
456
|
+
export const RoutingAnswer: z.ZodType<RoutingAnswer> = z.union([
|
|
457
|
+
z.object({
|
|
458
|
+
shell: z.object({ domain: hostName, home: z.string().min(1) }),
|
|
459
|
+
cacheForMs: cacheFor,
|
|
460
|
+
}),
|
|
461
|
+
z.object({
|
|
462
|
+
route: z.object({ home: z.string().min(1), routeId: z.string().min(1) }),
|
|
463
|
+
cacheForMs: cacheFor,
|
|
464
|
+
}),
|
|
465
|
+
z.null(),
|
|
466
|
+
]);
|
|
467
|
+
|
|
468
|
+
/** The schema of `routing.linkHost`'s answer: a shell host, or `null` for the active domain. */
|
|
469
|
+
export const LinkHost: z.ZodType<string | null> = hostName.nullable();
|
|
470
|
+
|
|
471
|
+
// ── events ──────────────────────────────────────────────────────────────────────────────
|
|
472
|
+
|
|
473
|
+
/** The kind of an outbound event. */
|
|
474
|
+
export type OperatorEventKind =
|
|
475
|
+
| "submission.accepted"
|
|
476
|
+
| "version.pushed"
|
|
477
|
+
| "run.ended"
|
|
478
|
+
| "roster.completed"
|
|
479
|
+
| (string & {});
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* One outbound event, delivered at least once with a stable `eventId`. `payload`:
|
|
483
|
+
* `submission.accepted` carries the submission's `Results` (INTERFACES §8, the `Results` type of `@mostra/protocol`); `version.pushed`
|
|
484
|
+
* `{ versionId, number }`; `run.ended` `{ runId, action, status }`; `roster.completed` is WP-31's.
|
|
485
|
+
* `label` is the route's label, so an operator running several servers behind one front learns
|
|
486
|
+
* which serves which label (WP-30).
|
|
487
|
+
*/
|
|
488
|
+
export interface OperatorEvent {
|
|
489
|
+
/** Stable across retries. */
|
|
490
|
+
readonly eventId: string;
|
|
491
|
+
/** What happened. */
|
|
492
|
+
readonly kind: OperatorEventKind;
|
|
493
|
+
/** The account that owns the route. */
|
|
494
|
+
readonly account: string;
|
|
495
|
+
/** Its hosted home. */
|
|
496
|
+
readonly home: string;
|
|
497
|
+
/** The route. */
|
|
498
|
+
readonly routeId: string;
|
|
499
|
+
/** The route's label. */
|
|
500
|
+
readonly label: string;
|
|
501
|
+
/** The artifact's own ID (as its manifest names it). */
|
|
502
|
+
readonly artifactId: string;
|
|
503
|
+
/** When it happened. */
|
|
504
|
+
readonly at: string;
|
|
505
|
+
/** What the kind carries. */
|
|
506
|
+
readonly payload: unknown;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ── the module ──────────────────────────────────────────────────────────────────────────
|
|
510
|
+
|
|
511
|
+
/** The ports an operator module implements; each is optional, with the default named. */
|
|
512
|
+
export interface OperatorPorts {
|
|
513
|
+
/** Allows or refuses an operation for an account. Default: allow. */
|
|
514
|
+
readonly limits?: { check(q: LimitQuery): Promise<LimitAnswer> };
|
|
515
|
+
/** Counters in batches, at least once. Default: discard. */
|
|
516
|
+
readonly metering?: { record(batch: MeterBatch): Promise<void> };
|
|
517
|
+
/**
|
|
518
|
+
* Identity providers beside the configured ones, asked at each sign-in so the set can change
|
|
519
|
+
* while the server runs (WP-30). Default: none.
|
|
520
|
+
*/
|
|
521
|
+
readonly identity?: {
|
|
522
|
+
providers(): Promise<readonly IdentityProvider[]> | readonly IdentityProvider[];
|
|
523
|
+
};
|
|
524
|
+
/**
|
|
525
|
+
* Sign-in identities to accounts, a first sign-in step, and credentials the operator issued.
|
|
526
|
+
* Default: the server's own accounts, no step, no outside credential.
|
|
527
|
+
*/
|
|
528
|
+
readonly accounts?: {
|
|
529
|
+
resolve(
|
|
530
|
+
identity: SignInIdentity,
|
|
531
|
+
context: { readonly steps: number },
|
|
532
|
+
): Promise<AccountResolution>;
|
|
533
|
+
authenticate?(
|
|
534
|
+
bearer: string,
|
|
535
|
+
context: { readonly resource: CredentialResource },
|
|
536
|
+
): Promise<AuthenticatedCredential | null>;
|
|
537
|
+
status?(account: string): Promise<"active" | "suspended">;
|
|
538
|
+
};
|
|
539
|
+
/** Reports and the after-push hook. Default: nothing. */
|
|
540
|
+
readonly abuse?: {
|
|
541
|
+
report(r: AbuseReport): Promise<void>;
|
|
542
|
+
pushed?(v: PushedVersion): Promise<PushedAnswer>;
|
|
543
|
+
};
|
|
544
|
+
/** Per link and per chat host. Default: nothing beyond configuration. */
|
|
545
|
+
readonly policy?: {
|
|
546
|
+
link(q: LinkQuery): Promise<LinkPolicy>;
|
|
547
|
+
chatHost?(client: {
|
|
548
|
+
readonly name: string;
|
|
549
|
+
readonly redirectHost?: string;
|
|
550
|
+
}): Promise<ChatHostPolicy | null>;
|
|
551
|
+
};
|
|
552
|
+
/**
|
|
553
|
+
* Hosts the operator supplies, and the host a new link is minted on. Default: the server's own
|
|
554
|
+
* labels, and links on the active domain.
|
|
555
|
+
*/
|
|
556
|
+
readonly routing?: {
|
|
557
|
+
resolve(host: string): Promise<RoutingAnswer>;
|
|
558
|
+
/**
|
|
559
|
+
* The shell host a new link of this route is minted on (one `resolve` answers `shell` or
|
|
560
|
+
* `route` for), or `null` for the active domain (WP-30). A link keeps the host it was minted
|
|
561
|
+
* on (D137).
|
|
562
|
+
*/
|
|
563
|
+
linkHost?(q: {
|
|
564
|
+
readonly account: string;
|
|
565
|
+
readonly home: string;
|
|
566
|
+
readonly routeId: string;
|
|
567
|
+
}): Promise<string | null>;
|
|
568
|
+
};
|
|
569
|
+
/** Outbound events, at least once with stable IDs. Default: discard. */
|
|
570
|
+
readonly events?: { deliver(e: OperatorEvent): Promise<void> };
|
|
571
|
+
/** Reserved for WP-31 (D121, D165 item 16); no mailer ships. */
|
|
572
|
+
readonly invitations?: unknown;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/** A hosted route, or one version of it: what the handle's quarantine and release act on. */
|
|
576
|
+
export type RouteTarget =
|
|
577
|
+
| { readonly routeId: string }
|
|
578
|
+
| { readonly routeId: string; readonly versionId: string };
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* What the server gives its operator module (INTERFACES §18.6). Every target names a hosted route.
|
|
582
|
+
* Each call is durable when it resolves.
|
|
583
|
+
*/
|
|
584
|
+
export interface ServerHandle {
|
|
585
|
+
/** The operator boundary's revision this server speaks. */
|
|
586
|
+
readonly revision: string;
|
|
587
|
+
/** The server's API origin. */
|
|
588
|
+
readonly apiOrigin: string;
|
|
589
|
+
/** A neutral "under review" page from the shell; data kept; `release` restores it. */
|
|
590
|
+
quarantine(target: RouteTarget, reason: string): Promise<void>;
|
|
591
|
+
/** Ends a quarantine. */
|
|
592
|
+
release(target: RouteTarget): Promise<void>;
|
|
593
|
+
/** Views keep working; submissions and relayed actions are refused; only the operator resumes. */
|
|
594
|
+
pause(routeId: string, reason: string): Promise<void>;
|
|
595
|
+
/** Ends an operator's pause (and an owner's, too). */
|
|
596
|
+
resume(routeId: string): Promise<void>;
|
|
597
|
+
/** Refuses the home's bindings, pushes, links and pages until `resumeHome`. */
|
|
598
|
+
suspendHome(home: string, reason: string): Promise<void>;
|
|
599
|
+
/** Ends a suspension. */
|
|
600
|
+
resumeHome(home: string): Promise<void>;
|
|
601
|
+
/** Revokes every link of a route; answers how many were live. */
|
|
602
|
+
revokeLinks(routeId: string): Promise<number>;
|
|
603
|
+
/** Revokes one link (410 from then on). */
|
|
604
|
+
revokeLink(tokenId: string): Promise<void>;
|
|
605
|
+
/** Closes the home and refuses to reopen it, to move it between data directories. */
|
|
606
|
+
drainHome(home: string): Promise<void>;
|
|
607
|
+
/** Serves a drained home again. */
|
|
608
|
+
releaseHome(home: string): Promise<void>;
|
|
609
|
+
/** A restore from a backup: rotates the home's incarnation before it serves anything. */
|
|
610
|
+
restored(home: string): Promise<void>;
|
|
611
|
+
/** Deletion: explicit, never part of another action. The route's label is never issued again. */
|
|
612
|
+
remove(routeId: string): Promise<void>;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** An operator module's default export. */
|
|
616
|
+
export type OperatorModule = (server: ServerHandle) => OperatorPorts | Promise<OperatorPorts>;
|
|
617
|
+
|
|
618
|
+
/** Types an operator module's default export; returns it unchanged. */
|
|
619
|
+
export function defineOperator(module: OperatorModule): OperatorModule {
|
|
620
|
+
return module;
|
|
621
|
+
}
|