@mostra/runtime 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin.js +930 -458
- package/dist/bin.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/{instance-DCiUHvgZ.js → instance-D5gh-sMM.js} +1231 -46
- package/dist/instance-D5gh-sMM.js.map +1 -0
- package/dist/reference/cli.md +112 -2
- package/dist/reference/diagnostics.json +29 -2
- package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +2 -2
- package/dist/reference/errors/MOSTRA_TEAM_PRESENCE_RATE_LIMITED.md +23 -0
- package/dist/reference/errors/MOSTRA_TEAM_ROSTER_POLICY.md +23 -0
- package/dist/reference/errors/MOSTRA_TEAM_ROSTER_TOO_LARGE.md +23 -0
- package/dist/reference/self-hosting.md +1 -1
- package/dist/skill/SKILL.md +5 -3
- package/dist/skill/references/answers.md +3 -1
- package/dist/skill/references/mcp.md +2 -2
- package/dist/skill/references/teams.md +78 -0
- package/package.json +6 -6
- package/src/actions/index.ts +5 -2
- package/src/gateway/actions.ts +6 -1
- package/src/gateway/browser.ts +84 -1
- package/src/gateway/capabilities.ts +5 -1
- package/src/gateway/index.ts +43 -1
- package/src/gateway/team.ts +181 -0
- package/src/instance/index.ts +2 -1
- package/src/interaction/ephemeral.ts +21 -0
- package/src/interaction/events.ts +17 -0
- package/src/interaction/index.ts +265 -20
- package/src/interaction/roster.ts +585 -0
- package/src/interaction/team.ts +426 -0
- package/src/interaction/wait.ts +150 -0
- package/src/ledger/migrations.ts +44 -0
- package/src/link/hosted.ts +14 -3
- package/src/link/manager.ts +31 -1
- package/src/server/api.ts +1 -1
- package/src/server/auth.ts +83 -16
- package/src/server/authorize.ts +27 -4
- package/src/server/config.ts +53 -0
- package/src/server/directory.ts +73 -0
- package/src/server/feed.ts +89 -3
- package/src/server/hosted-pages.ts +19 -6
- package/src/server/hosted.ts +108 -49
- package/src/server/index.ts +72 -15
- package/src/server/metering.ts +117 -12
- package/src/server/operator.ts +87 -28
- package/src/server/providers.ts +61 -9
- package/src/server/relay.ts +258 -107
- package/src/share/index.ts +50 -2
- package/src/verify/index.ts +7 -0
- package/src/verify/runner.ts +5 -1
- package/dist/instance-DCiUHvgZ.js.map +0 -1
package/src/gateway/browser.ts
CHANGED
|
@@ -7,12 +7,15 @@
|
|
|
7
7
|
import {
|
|
8
8
|
BROWSER_PROTOCOL_REVISIONS,
|
|
9
9
|
ChooseRequest,
|
|
10
|
+
createDiagnostic,
|
|
10
11
|
type Diagnostic,
|
|
11
12
|
type EphemeralMessage,
|
|
12
13
|
type EventRecord,
|
|
13
14
|
FieldsRequest,
|
|
14
15
|
FollowRequest,
|
|
16
|
+
PresenceRequest,
|
|
15
17
|
type Refusal,
|
|
18
|
+
RespondentsRequest,
|
|
16
19
|
RpcError,
|
|
17
20
|
refusal,
|
|
18
21
|
ShellSubmitRequest,
|
|
@@ -28,6 +31,7 @@ import {
|
|
|
28
31
|
type Interaction,
|
|
29
32
|
type PageIdentity,
|
|
30
33
|
SubmitConflict,
|
|
34
|
+
type TeamTraffic,
|
|
31
35
|
} from "../interaction/index.ts";
|
|
32
36
|
import type { Log } from "../log.ts";
|
|
33
37
|
import type { Capabilities } from "./capabilities.ts";
|
|
@@ -67,6 +71,8 @@ const STATUS: Readonly<Record<string, number>> = {
|
|
|
67
71
|
MOSTRA_HYGIENE_SCOPE_DENIED: 403,
|
|
68
72
|
// A paused artifact takes no answers (D165 item 14).
|
|
69
73
|
MOSTRA_SUBMIT_PAUSED: 423,
|
|
74
|
+
// A page posting presence faster than its rate (D168).
|
|
75
|
+
MOSTRA_TEAM_PRESENCE_RATE_LIMITED: 429,
|
|
70
76
|
};
|
|
71
77
|
|
|
72
78
|
/** Options for {@link browserRoutes}. */
|
|
@@ -89,6 +95,11 @@ export interface BrowserRoutesOptions {
|
|
|
89
95
|
* without an `id`, a poll as its `ephemeral` (LL-04). Absent: neither carries any.
|
|
90
96
|
*/
|
|
91
97
|
readonly bus?: EphemeralBus;
|
|
98
|
+
/**
|
|
99
|
+
* Respondents and presence for pages (D168): absent, the `respondents` and `presence` routes
|
|
100
|
+
* answer that this gateway serves neither.
|
|
101
|
+
*/
|
|
102
|
+
readonly team?: TeamTraffic;
|
|
92
103
|
/**
|
|
93
104
|
* Before a submit, from the frame or the shell (a remote server's, D165 item 13): a refusal (with
|
|
94
105
|
* when to try again) keeps the draft; nothing is recorded. Absent: every submit goes on.
|
|
@@ -120,6 +131,20 @@ function malformed(message: string): Response {
|
|
|
120
131
|
return answer(refusal("MOSTRA_HYGIENE_REQUEST_MALFORMED", message).diagnostic, 400);
|
|
121
132
|
}
|
|
122
133
|
|
|
134
|
+
/** What an ephemeral message's SSE `data` carries: the message's own payload, by its kind. */
|
|
135
|
+
function ephemeralData(message: EphemeralMessage): unknown {
|
|
136
|
+
switch (message.kind) {
|
|
137
|
+
case "run":
|
|
138
|
+
return message.run;
|
|
139
|
+
case "live":
|
|
140
|
+
return message.live;
|
|
141
|
+
case "respondents":
|
|
142
|
+
return message.respondents;
|
|
143
|
+
case "presence":
|
|
144
|
+
return message.presence;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
123
148
|
/** Turns an interaction refusal into its response; anything else is a defect for `onError`. */
|
|
124
149
|
function failure(error: unknown): Response {
|
|
125
150
|
if (error instanceof SubmitConflict) {
|
|
@@ -191,6 +216,10 @@ export interface BrowserRoutes {
|
|
|
191
216
|
/** `events/poll` on the shell origin: the same, without the frame's run and live updates (#119). */
|
|
192
217
|
readonly shellPoll: Handler;
|
|
193
218
|
readonly shellSubmit: Handler;
|
|
219
|
+
/** The roster as the page may see it (D168, CO-03, CO-07). */
|
|
220
|
+
readonly respondents: Handler;
|
|
221
|
+
/** The page's presence (D168, CO-04). */
|
|
222
|
+
readonly presence: Handler;
|
|
194
223
|
/** The shell's trusted Choose (D65, D113), on the shell origin. */
|
|
195
224
|
readonly choose: Handler;
|
|
196
225
|
/** "Always follow this artifact" (D63), on the shell origin. */
|
|
@@ -376,7 +405,7 @@ export function browserRoutes(options: BrowserRoutesOptions): BrowserRoutes {
|
|
|
376
405
|
.splice(0)
|
|
377
406
|
.map(
|
|
378
407
|
(message) =>
|
|
379
|
-
`event: ${message.kind}\ndata: ${JSON.stringify(message
|
|
408
|
+
`event: ${message.kind}\ndata: ${JSON.stringify(ephemeralData(message))}\n\n`,
|
|
380
409
|
)
|
|
381
410
|
.join("");
|
|
382
411
|
const unlisten =
|
|
@@ -704,6 +733,58 @@ export function browserRoutes(options: BrowserRoutesOptions): BrowserRoutes {
|
|
|
704
733
|
}
|
|
705
734
|
};
|
|
706
735
|
|
|
736
|
+
/** A handler of a team route without team traffic: this gateway serves neither (verify's). */
|
|
737
|
+
const noTeam = (): Response =>
|
|
738
|
+
answer(
|
|
739
|
+
refusal("MOSTRA_HYGIENE_NOT_SERVED", "This gateway serves no respondents or presence.", 404)
|
|
740
|
+
.diagnostic,
|
|
741
|
+
404,
|
|
742
|
+
);
|
|
743
|
+
|
|
744
|
+
/** `respondents` (D168): the roster as the page may see it, followed for a minute. */
|
|
745
|
+
const respondents = async (context: RouteContext): Promise<Response> => {
|
|
746
|
+
const refused = knownRevision(context);
|
|
747
|
+
if (refused) return refused;
|
|
748
|
+
if (!options.team) return noTeam();
|
|
749
|
+
const request = await body(context, RespondentsRequest);
|
|
750
|
+
if (request instanceof Response) return request;
|
|
751
|
+
const page = pageOf(context);
|
|
752
|
+
try {
|
|
753
|
+
interaction.checkView(page, request.viewId);
|
|
754
|
+
return jsonResponse(options.team.respondents(page, request.viewId));
|
|
755
|
+
} catch (error) {
|
|
756
|
+
return failure(error);
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
/** `presence` (D168, CO-04): joins, shares or leaves; never stored. */
|
|
761
|
+
const presence = async (context: RouteContext): Promise<Response> => {
|
|
762
|
+
const refused = knownRevision(context);
|
|
763
|
+
if (refused) return refused;
|
|
764
|
+
if (!options.team) return noTeam();
|
|
765
|
+
const request = await body(context, PresenceRequest);
|
|
766
|
+
if (request instanceof Response) return request;
|
|
767
|
+
const page = pageOf(context);
|
|
768
|
+
try {
|
|
769
|
+
interaction.checkView(page, request.viewId);
|
|
770
|
+
} catch (error) {
|
|
771
|
+
return failure(error);
|
|
772
|
+
}
|
|
773
|
+
const outcome = options.team.presence(page, request);
|
|
774
|
+
if ("limited" in outcome) {
|
|
775
|
+
return jsonResponse(
|
|
776
|
+
createDiagnostic("MOSTRA_TEAM_PRESENCE_RATE_LIMITED", {
|
|
777
|
+
message: "This page sends presence faster than 20 updates a second.",
|
|
778
|
+
}),
|
|
779
|
+
{
|
|
780
|
+
status: 429,
|
|
781
|
+
headers: { "retry-after": String(Math.max(1, Math.ceil(outcome.retryAfterMs / 1000))) },
|
|
782
|
+
},
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
return jsonResponse(outcome.state);
|
|
786
|
+
};
|
|
787
|
+
|
|
707
788
|
return {
|
|
708
789
|
fields,
|
|
709
790
|
submit,
|
|
@@ -711,6 +792,8 @@ export function browserRoutes(options: BrowserRoutesOptions): BrowserRoutes {
|
|
|
711
792
|
stream,
|
|
712
793
|
poll: pollFor(true),
|
|
713
794
|
shellPoll: pollFor(false),
|
|
795
|
+
respondents,
|
|
796
|
+
presence,
|
|
714
797
|
shellSubmit,
|
|
715
798
|
choose,
|
|
716
799
|
follow,
|
|
@@ -266,6 +266,8 @@ export interface CapabilitiesOptions {
|
|
|
266
266
|
* links, so its operator's `revokeLink` finds a link's home, WP-30).
|
|
267
267
|
*/
|
|
268
268
|
readonly onMinted?: (token: { readonly tokenId: string; readonly artifactId: string }) => void;
|
|
269
|
+
/** How often a session's `last_seen_at` is written at most; an hour unless a test shortens it. */
|
|
270
|
+
readonly touchIntervalMs?: number;
|
|
269
271
|
}
|
|
270
272
|
|
|
271
273
|
/** What a stream ticket opens (D158 item 5). */
|
|
@@ -302,6 +304,7 @@ export class Capabilities {
|
|
|
302
304
|
/** Failure times per artifact (`""` for tokens of no artifact in this home). */
|
|
303
305
|
readonly #failures = new Map<string, number[]>();
|
|
304
306
|
readonly #touched = new Map<string, number>();
|
|
307
|
+
readonly #touchIntervalMs: number;
|
|
305
308
|
|
|
306
309
|
constructor(options: CapabilitiesOptions) {
|
|
307
310
|
this.#ledger = options.ledger;
|
|
@@ -311,6 +314,7 @@ export class Capabilities {
|
|
|
311
314
|
this.#shellOrigin = options.shellOrigin;
|
|
312
315
|
this.#servesShell = options.servesShell ?? ((origin) => origin === this.#shellOrigin());
|
|
313
316
|
this.#onMinted = options.onMinted;
|
|
317
|
+
this.#touchIntervalMs = options.touchIntervalMs ?? TOUCH_INTERVAL_MS;
|
|
314
318
|
}
|
|
315
319
|
|
|
316
320
|
/** Runs `fn` in the caller's transaction, or in a new one. */
|
|
@@ -731,7 +735,7 @@ export class Capabilities {
|
|
|
731
735
|
/** Writes `last_seen_at` at most hourly, so serving files doesn't write the ledger per request. */
|
|
732
736
|
#touch(sessionId: string): void {
|
|
733
737
|
const now = this.#now();
|
|
734
|
-
if (now - (this.#touched.get(sessionId) ?? 0) <
|
|
738
|
+
if (now - (this.#touched.get(sessionId) ?? 0) < this.#touchIntervalMs) return;
|
|
735
739
|
this.#touched.set(sessionId, now);
|
|
736
740
|
this.#ledger.write((db) =>
|
|
737
741
|
db.run("UPDATE session SET last_seen_at = ? WHERE id = ?", this.#iso(now), sessionId),
|
package/src/gateway/index.ts
CHANGED
|
@@ -63,7 +63,7 @@ import {
|
|
|
63
63
|
import type { Actions } from "../actions/index.ts";
|
|
64
64
|
import { type Addressing, type FrameMode, hostOf } from "../addressing/index.ts";
|
|
65
65
|
import { runtimeRoot } from "../deps/index.ts";
|
|
66
|
-
import type
|
|
66
|
+
import { createTeamTraffic, type EphemeralBus, type Interaction } from "../interaction/index.ts";
|
|
67
67
|
import {
|
|
68
68
|
type Ledger,
|
|
69
69
|
type LedgerDb,
|
|
@@ -103,6 +103,7 @@ export { resetOnClose } from "./listener.ts";
|
|
|
103
103
|
|
|
104
104
|
import { type ListenerChecks, mount, type RouteContext, type RouteSpec, refuse } from "./routes.ts";
|
|
105
105
|
import { fileResponse, findFile } from "./serve.ts";
|
|
106
|
+
import { rosterMethods } from "./team.ts";
|
|
106
107
|
import { BOOTSTRAP_PAGE, SERVICE_WORKER_PATH, SERVICE_WORKER_SOURCE } from "./worker.ts";
|
|
107
108
|
|
|
108
109
|
export { Capabilities, capabilityKey } from "./capabilities.ts";
|
|
@@ -227,6 +228,12 @@ export interface GatewayOptions {
|
|
|
227
228
|
readonly live?: Pick<Live, "read">;
|
|
228
229
|
/** Run and live updates for page streams and polls (WP-08). */
|
|
229
230
|
readonly bus?: EphemeralBus;
|
|
231
|
+
/**
|
|
232
|
+
* Asks whether `count` new links may be minted for an artifact, throwing to refuse: a remote
|
|
233
|
+
* server's operator `limits` (`link`, D165 item 9). `roster/add` asks it for exactly the
|
|
234
|
+
* personal links it will mint (Opus review 10).
|
|
235
|
+
*/
|
|
236
|
+
readonly linkLimit?: (artifactId: string, count: number) => Promise<void>;
|
|
230
237
|
/** Whether a version's worker is parked (WP-08): its frame ticket tells the shell. */
|
|
231
238
|
readonly parked?: (versionId: string) => boolean;
|
|
232
239
|
/**
|
|
@@ -288,6 +295,8 @@ export interface HostedAddressing {
|
|
|
288
295
|
readonly artifactId: string;
|
|
289
296
|
readonly versionId: string;
|
|
290
297
|
readonly tokenId: string;
|
|
298
|
+
/** The shell session asking for the ticket: its own activity is this visit's (MT-03). */
|
|
299
|
+
readonly sessionId: string;
|
|
291
300
|
readonly client: string | undefined;
|
|
292
301
|
}) => Promise<(Refusal & { readonly retryAfter?: number }) | undefined>;
|
|
293
302
|
/** A version whose files are withheld now (a quarantine): refused even to an open frame. */
|
|
@@ -495,6 +504,21 @@ function laterArtifactRoutes(
|
|
|
495
504
|
auth: "session",
|
|
496
505
|
...(acting ? { handler: acting.live } : {}),
|
|
497
506
|
},
|
|
507
|
+
// WP-31: the roster as the page may see it, and presence; every link may (`view`, D168).
|
|
508
|
+
{
|
|
509
|
+
method: "POST",
|
|
510
|
+
path: "/_mostra/v/:rev/respondents",
|
|
511
|
+
intent: "mutation",
|
|
512
|
+
auth: "session",
|
|
513
|
+
handler: served.respondents,
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
method: "POST",
|
|
517
|
+
path: "/_mostra/v/:rev/presence",
|
|
518
|
+
intent: "mutation",
|
|
519
|
+
auth: "session",
|
|
520
|
+
handler: served.presence,
|
|
521
|
+
},
|
|
498
522
|
// WP-13: single-use tickets for streams, polls and WebSockets that bypass the worker (D70).
|
|
499
523
|
{
|
|
500
524
|
method: "POST",
|
|
@@ -533,7 +557,12 @@ interface FaceEntry {
|
|
|
533
557
|
/** Creates the gateway; nothing is bound until {@link Gateway.start}. */
|
|
534
558
|
export function createGateway(options: GatewayOptions): Gateway {
|
|
535
559
|
const { addressing, capabilities, artifacts, log, interaction } = options;
|
|
560
|
+
// Respondents and presence for pages (D168): in memory, over the pages' ephemeral bus.
|
|
561
|
+
const team = options.bus
|
|
562
|
+
? createTeamTraffic({ bus: options.bus, source: interaction, log })
|
|
563
|
+
: undefined;
|
|
536
564
|
const served = browserRoutes({
|
|
565
|
+
...(team ? { team } : {}),
|
|
537
566
|
interaction,
|
|
538
567
|
capabilities,
|
|
539
568
|
log,
|
|
@@ -1378,6 +1407,7 @@ export function createGateway(options: GatewayOptions): Gateway {
|
|
|
1378
1407
|
artifactId,
|
|
1379
1408
|
versionId: version.id,
|
|
1380
1409
|
tokenId: session.grant.tokenId,
|
|
1410
|
+
sessionId: session.id,
|
|
1381
1411
|
client: context.client,
|
|
1382
1412
|
});
|
|
1383
1413
|
if (refused) return refuse(checks, refused, meta);
|
|
@@ -2180,6 +2210,17 @@ export function createGateway(options: GatewayOptions): Gateway {
|
|
|
2180
2210
|
by: entry.spec.by,
|
|
2181
2211
|
})),
|
|
2182
2212
|
methods: () => [
|
|
2213
|
+
...rosterMethods({
|
|
2214
|
+
ledger: options.ledger,
|
|
2215
|
+
interaction,
|
|
2216
|
+
capabilities,
|
|
2217
|
+
artifacts,
|
|
2218
|
+
versionOf,
|
|
2219
|
+
linkBase,
|
|
2220
|
+
link,
|
|
2221
|
+
collected: (number) => new RpcError(collectedDiagnostic(number)),
|
|
2222
|
+
...(options.linkLimit ? { linkLimit: options.linkLimit } : {}),
|
|
2223
|
+
}),
|
|
2183
2224
|
handler(viewUrlMethod, async (params) => {
|
|
2184
2225
|
const detail = artifacts.get(params.artifact);
|
|
2185
2226
|
const version = versionOf(detail, params.version);
|
|
@@ -2286,6 +2327,7 @@ export function createGateway(options: GatewayOptions): Gateway {
|
|
|
2286
2327
|
]),
|
|
2287
2328
|
),
|
|
2288
2329
|
async close() {
|
|
2330
|
+
team?.close();
|
|
2289
2331
|
await Promise.allSettled([...opening.values(), ...openingFaces.values()]);
|
|
2290
2332
|
await Promise.all([
|
|
2291
2333
|
shellListener?.close(),
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The roster's host methods (D121, D168; CO-01, CO-03, CO-07): `roster/add` names expected
|
|
3
|
+
* respondents and gives each a personal link, `roster/remove` stops expecting them, `roster/get`
|
|
4
|
+
* reads everyone's status. They live with the gateway because a personal link is a capability
|
|
5
|
+
* link (D97): minted here, with the respondent's viewer (D104), in the same transaction as the
|
|
6
|
+
* roster's change. The roster itself, its rounds and its completion are the interaction core's.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
type Actor,
|
|
10
|
+
ALL_SCOPES,
|
|
11
|
+
type ArtifactDetail,
|
|
12
|
+
type ArtifactRef,
|
|
13
|
+
type CapabilityLink,
|
|
14
|
+
createDiagnostic,
|
|
15
|
+
durationMs,
|
|
16
|
+
normalizeScopes,
|
|
17
|
+
type Roster,
|
|
18
|
+
RpcError,
|
|
19
|
+
rosterAddMethod,
|
|
20
|
+
rosterGetMethod,
|
|
21
|
+
rosterRemoveMethod,
|
|
22
|
+
} from "@mostra/protocol";
|
|
23
|
+
import type { Interaction } from "../interaction/index.ts";
|
|
24
|
+
import { type Ledger, recordedResult, recordResult, requestKey } from "../ledger/index.ts";
|
|
25
|
+
import { handler, type MethodHandler, type Session } from "../protocol-server/index.ts";
|
|
26
|
+
import type { Capabilities, MintedToken } from "./capabilities.ts";
|
|
27
|
+
|
|
28
|
+
/** What the roster methods need of the gateway. */
|
|
29
|
+
export interface RosterMethodsOptions {
|
|
30
|
+
readonly ledger: Ledger;
|
|
31
|
+
readonly interaction: Interaction;
|
|
32
|
+
readonly capabilities: Capabilities;
|
|
33
|
+
readonly artifacts: { get(ref: ArtifactRef): ArtifactDetail };
|
|
34
|
+
/** The version a request names, or the latest. */
|
|
35
|
+
readonly versionOf: (
|
|
36
|
+
detail: ArtifactDetail,
|
|
37
|
+
wanted: number | string | undefined,
|
|
38
|
+
) => { readonly id: string; readonly number: number; readonly collected?: string } | undefined;
|
|
39
|
+
/** Where the artifact's links point now (D97 item 10). */
|
|
40
|
+
readonly linkBase: (artifactId: string) => Promise<string>;
|
|
41
|
+
/** A minted token as a capability link. */
|
|
42
|
+
readonly link: (token: MintedToken) => CapabilityLink;
|
|
43
|
+
/** What a link refused for its collected version says. */
|
|
44
|
+
readonly collected: (number: number) => RpcError;
|
|
45
|
+
/** Asks whether `count` new links may be minted, throwing to refuse (an operator's `limits`). */
|
|
46
|
+
readonly linkLimit?: (artifactId: string, count: number) => Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Who changed the roster: the client that called. */
|
|
50
|
+
function actorOf(session: Session): Actor {
|
|
51
|
+
return { kind: "agent", ...(session.client ? { id: session.client.name } : {}) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The roster's host methods. */
|
|
55
|
+
export function rosterMethods(options: RosterMethodsOptions): MethodHandler[] {
|
|
56
|
+
const { ledger, interaction, capabilities } = options;
|
|
57
|
+
|
|
58
|
+
const live = (tokenId: string): boolean => capabilities.grant(tokenId)?.live === true;
|
|
59
|
+
const roster = (artifactId: string): Roster => interaction.roster(artifactId, live);
|
|
60
|
+
|
|
61
|
+
return [
|
|
62
|
+
handler(rosterAddMethod, async (params, session) => {
|
|
63
|
+
const detail = options.artifacts.get(params.artifact);
|
|
64
|
+
const version = options.versionOf(detail, params.version);
|
|
65
|
+
if (!version) {
|
|
66
|
+
throw new RpcError(
|
|
67
|
+
createDiagnostic("MOSTRA_ARTIFACT_VERSION_NOT_FOUND", {
|
|
68
|
+
message:
|
|
69
|
+
params.version === undefined
|
|
70
|
+
? `The artifact ${detail.id} has no published version.`
|
|
71
|
+
: `The artifact ${detail.id} has no version ${params.version}.`,
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (version.collected !== undefined) throw options.collected(version.number);
|
|
76
|
+
const base = await options.linkBase(detail.id);
|
|
77
|
+
const scopes = normalizeScopes(params.scopes ?? ALL_SCOPES).join(" ");
|
|
78
|
+
const pinned = params.version === undefined ? undefined : version.number;
|
|
79
|
+
/**
|
|
80
|
+
* Whether a respondent's link is the one this request would mint: it works here (D97 items
|
|
81
|
+
* 5, 10) with the same scopes, the same pinned version, and an expiry exactly when a `ttl` is
|
|
82
|
+
* asked for (Opus review 6). Otherwise the respondent gets a new link; the old one isn't
|
|
83
|
+
* revoked, since they may hold it already.
|
|
84
|
+
*/
|
|
85
|
+
const reusable = (tokenId: string): boolean => {
|
|
86
|
+
const grant = capabilities.grant(tokenId);
|
|
87
|
+
return (
|
|
88
|
+
grant?.live === true &&
|
|
89
|
+
grant.shellOrigin === base &&
|
|
90
|
+
normalizeScopes(grant.scopes).join(" ") === scopes &&
|
|
91
|
+
grant.pinnedVersion === pinned &&
|
|
92
|
+
(grant.expiresAt !== undefined) === (params.ttl !== undefined)
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
const key = requestKey("roster/add", params);
|
|
96
|
+
// An operator's limit counts the links this call will mint: respondents whose link is kept
|
|
97
|
+
// mint none, and a replay of a recorded call mints nothing (Opus review 10).
|
|
98
|
+
if (options.linkLimit) {
|
|
99
|
+
const replay =
|
|
100
|
+
key !== undefined && ledger.read((db) => recordedResult(db, key)) !== undefined;
|
|
101
|
+
if (!replay) {
|
|
102
|
+
const current = new Map(
|
|
103
|
+
interaction.roster(detail.id, live).respondents.map((one) => [one.ref, one.tokenId]),
|
|
104
|
+
);
|
|
105
|
+
let fresh = 0;
|
|
106
|
+
for (const ref of new Set(params.respondents.map((one) => one.ref))) {
|
|
107
|
+
const tokenId = current.get(ref);
|
|
108
|
+
if (tokenId === undefined || !reusable(tokenId)) fresh += 1;
|
|
109
|
+
}
|
|
110
|
+
if (fresh > 0) await options.linkLimit(detail.id, fresh);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const minted = new Map<string, MintedToken>();
|
|
114
|
+
const tokens = ledger.write((db) => {
|
|
115
|
+
if (key !== undefined) {
|
|
116
|
+
const row = recordedResult<{ tokens: { ref: string; tokenId: string }[] }>(db, key);
|
|
117
|
+
if (row) return row.tokens;
|
|
118
|
+
}
|
|
119
|
+
const added = interaction.addRespondents(db, {
|
|
120
|
+
artifactId: detail.id,
|
|
121
|
+
respondents: params.respondents,
|
|
122
|
+
actor: actorOf(session),
|
|
123
|
+
// A respondent keeps the personal link they have while it is the one this request would
|
|
124
|
+
// mint; a revoked, expired or moved one, or one of other scopes, version or expiry, is
|
|
125
|
+
// replaced by a new one for the same viewer.
|
|
126
|
+
link: (viewerId, current) => {
|
|
127
|
+
if (current !== undefined && reusable(current)) return current;
|
|
128
|
+
const token = capabilities.mint(
|
|
129
|
+
{
|
|
130
|
+
artifactId: detail.id,
|
|
131
|
+
scopes: params.scopes,
|
|
132
|
+
ttlMs: params.ttl === undefined ? undefined : durationMs(params.ttl),
|
|
133
|
+
pinnedVersion: pinned,
|
|
134
|
+
viewerId,
|
|
135
|
+
shellOrigin: base,
|
|
136
|
+
},
|
|
137
|
+
db,
|
|
138
|
+
);
|
|
139
|
+
minted.set(token.tokenId, token);
|
|
140
|
+
return token.tokenId;
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
const recorded = added.tokens.map((token) => ({ ref: token.ref, tokenId: token.tokenId }));
|
|
144
|
+
// Only token IDs are recorded: the ledger never holds a link's secret (D97 item 2).
|
|
145
|
+
if (key !== undefined) recordResult(db, key, { tokens: recorded });
|
|
146
|
+
return recorded;
|
|
147
|
+
});
|
|
148
|
+
const links = tokens.map(({ ref, tokenId }) => {
|
|
149
|
+
const token = minted.get(tokenId) ?? capabilities.reveal(tokenId);
|
|
150
|
+
if (!token) {
|
|
151
|
+
throw new RpcError(
|
|
152
|
+
createDiagnostic("MOSTRA_HYGIENE_TOKEN_REVOKED", {
|
|
153
|
+
message: `The personal link of ${ref} can't be shown again: the home's capability key changed. Add them again to give them a new one.`,
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
return { ref, link: options.link(token) };
|
|
158
|
+
});
|
|
159
|
+
return { roster: roster(detail.id), links };
|
|
160
|
+
}),
|
|
161
|
+
handler(rosterRemoveMethod, (params, session) => {
|
|
162
|
+
const detail = options.artifacts.get(params.artifact);
|
|
163
|
+
const key = requestKey("roster/remove", params);
|
|
164
|
+
const removed = ledger.write((db) => {
|
|
165
|
+
if (key !== undefined) {
|
|
166
|
+
const row = recordedResult<{ removed: string[] }>(db, key);
|
|
167
|
+
if (row) return row.removed;
|
|
168
|
+
}
|
|
169
|
+
const outcome = interaction.removeRespondents(db, {
|
|
170
|
+
artifactId: detail.id,
|
|
171
|
+
refs: params.refs,
|
|
172
|
+
actor: actorOf(session),
|
|
173
|
+
});
|
|
174
|
+
if (key !== undefined) recordResult(db, key, { removed: outcome.removed });
|
|
175
|
+
return [...outcome.removed];
|
|
176
|
+
});
|
|
177
|
+
return { roster: roster(detail.id), removed };
|
|
178
|
+
}),
|
|
179
|
+
handler(rosterGetMethod, (params) => roster(options.artifacts.get(params.artifact).id)),
|
|
180
|
+
];
|
|
181
|
+
}
|
package/src/instance/index.ts
CHANGED
|
@@ -893,7 +893,8 @@ async function assemble(home: OwnedHome, options: StartInstanceOptions): Promise
|
|
|
893
893
|
dependenciesAdded: (artifact) => dev.dependenciesChanged(artifact),
|
|
894
894
|
}),
|
|
895
895
|
...dev.methods(),
|
|
896
|
-
|
|
896
|
+
// A hosted artifact's roster is its hosted home's, like its state (D165 item 1, D168).
|
|
897
|
+
...forwardHosted(link, (ref) => registry.get(canonicalRef(ref)).id, gateway.methods()),
|
|
897
898
|
...shares.methods(),
|
|
898
899
|
...link.methods(),
|
|
899
900
|
// A hosted artifact's state and live values act on its hosted home (D165 item 1).
|
|
@@ -46,6 +46,14 @@ function coalesceInto(
|
|
|
46
46
|
queue: EphemeralMessage[],
|
|
47
47
|
message: EphemeralMessage,
|
|
48
48
|
): void {
|
|
49
|
+
if (message.kind === "respondents" || message.kind === "presence") {
|
|
50
|
+
// A page's roster and presence are whole states: the newest replaces any still queued.
|
|
51
|
+
for (let index = queue.length - 1; index >= 0; index--) {
|
|
52
|
+
if ((queue[index] as EphemeralMessage).kind === message.kind) queue.splice(index, 1);
|
|
53
|
+
}
|
|
54
|
+
queue.push(message);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
49
57
|
if (message.kind === "live") {
|
|
50
58
|
const latestMode = view.live.get(message.live.key)?.window === undefined;
|
|
51
59
|
if (latestMode && message.live.value !== undefined) {
|
|
@@ -124,6 +132,15 @@ export interface EphemeralBus {
|
|
|
124
132
|
publishRun(artifactId: string, message: Extract<EphemeralMessage, { kind: "run" }>): void;
|
|
125
133
|
/** Sends a live update to every view reading the source. */
|
|
126
134
|
publishLive(artifactId: string, message: Extract<EphemeralMessage, { kind: "live" }>): void;
|
|
135
|
+
/**
|
|
136
|
+
* Sends one view its own roster or presence (D168), when it listens or polls now: each view's
|
|
137
|
+
* is its own, under the visibility policy.
|
|
138
|
+
*/
|
|
139
|
+
send(
|
|
140
|
+
artifactId: string,
|
|
141
|
+
viewId: string,
|
|
142
|
+
message: Extract<EphemeralMessage, { kind: "respondents" | "presence" }>,
|
|
143
|
+
): void;
|
|
127
144
|
/** Hears the view's messages as they come (an open event stream); returns the stop. */
|
|
128
145
|
listen(
|
|
129
146
|
artifactId: string,
|
|
@@ -256,6 +273,10 @@ export function createEphemeralBus(
|
|
|
256
273
|
if ((view.live.get(message.live.key)?.until ?? 0) > at) deliver(view, message);
|
|
257
274
|
}
|
|
258
275
|
},
|
|
276
|
+
send(artifactId, viewId, message) {
|
|
277
|
+
const view = views.get(viewId);
|
|
278
|
+
if (view && view.artifactId === artifactId) deliver(view, message);
|
|
279
|
+
},
|
|
259
280
|
listen(artifactId, viewId, listener) {
|
|
260
281
|
const view = viewOf(artifactId, viewId);
|
|
261
282
|
view.listeners.add(listener);
|
|
@@ -308,6 +308,11 @@ export interface CompactionSummary {
|
|
|
308
308
|
readonly number: unknown;
|
|
309
309
|
readonly cursor: number;
|
|
310
310
|
};
|
|
311
|
+
/**
|
|
312
|
+
* Where the roster stood at the range's end (D168): whether it was complete, and its latest
|
|
313
|
+
* completion, so `submitted:all` still reads it once its events are replaced.
|
|
314
|
+
*/
|
|
315
|
+
readonly roster?: { readonly complete: boolean; readonly completed?: EventRecord };
|
|
311
316
|
}
|
|
312
317
|
|
|
313
318
|
/**
|
|
@@ -335,6 +340,7 @@ export function compactEvents(
|
|
|
335
340
|
let removed = 0;
|
|
336
341
|
let from = first.cursor;
|
|
337
342
|
let latestVersion: CompactionSummary["latestVersion"];
|
|
343
|
+
let roster: CompactionSummary["roster"];
|
|
338
344
|
for (const row of rows) {
|
|
339
345
|
if (row.type === COMPACTED) {
|
|
340
346
|
// An earlier summary folds into this one, with everything it covered.
|
|
@@ -344,10 +350,20 @@ export function compactEvents(
|
|
|
344
350
|
for (const [type, count] of Object.entries(earlier.types))
|
|
345
351
|
types[type] = (types[type] ?? 0) + count;
|
|
346
352
|
if (earlier.latestVersion) latestVersion = earlier.latestVersion;
|
|
353
|
+
if (earlier.roster) roster = earlier.roster;
|
|
347
354
|
continue;
|
|
348
355
|
}
|
|
349
356
|
removed += 1;
|
|
350
357
|
types[row.type] = (types[row.type] ?? 0) + 1;
|
|
358
|
+
if (row.type === "roster.completed") {
|
|
359
|
+
roster = { complete: true, completed: eventRecord(row) };
|
|
360
|
+
} else if (row.type === "roster.changed") {
|
|
361
|
+
const complete = (JSON.parse(row.payload_json) as { complete?: unknown }).complete === true;
|
|
362
|
+
roster = {
|
|
363
|
+
complete,
|
|
364
|
+
...(complete && roster?.completed ? { completed: roster.completed } : {}),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
351
367
|
if (row.type === "version.published") {
|
|
352
368
|
const payload = JSON.parse(row.payload_json) as { versionId?: unknown; number?: unknown };
|
|
353
369
|
latestVersion = { versionId: payload.versionId, number: payload.number, cursor: row.cursor };
|
|
@@ -359,6 +375,7 @@ export function compactEvents(
|
|
|
359
375
|
removed,
|
|
360
376
|
types,
|
|
361
377
|
...(latestVersion ? { latestVersion } : {}),
|
|
378
|
+
...(roster ? { roster } : {}),
|
|
362
379
|
};
|
|
363
380
|
db.run("DELETE FROM event WHERE artifact_id = ? AND cursor < ?", artifactId, before);
|
|
364
381
|
db.run(
|