@mostra/conformance 0.0.0-placeholder → 0.2.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 +68 -1
- package/baselines/reference-host-rust-broken.json +8 -0
- package/baselines/reference-host-ts-broken.json +8 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +106 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +403 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/protocol-RQ9lbehE.js +207 -0
- package/dist/protocol-RQ9lbehE.js.map +1 -0
- package/dist/reference-host-BJ-A_Qd_.d.ts +257 -0
- package/dist/reference-host-BJ-A_Qd_.d.ts.map +1 -0
- package/dist/reference-host-d6gs3Tal.js +409 -0
- package/dist/reference-host-d6gs3Tal.js.map +1 -0
- package/dist/reference-host.d.ts +2 -0
- package/dist/reference-host.js +3 -0
- package/dist/runner-CvoSJlfk.js +1940 -0
- package/dist/runner-CvoSJlfk.js.map +1 -0
- package/package.json +67 -4
- package/src/baseline.ts +51 -0
- package/src/cli.ts +117 -0
- package/src/fixtures.ts +87 -0
- package/src/harness/client.ts +224 -0
- package/src/harness/protocol.ts +381 -0
- package/src/harness/serve.ts +146 -0
- package/src/index.ts +65 -0
- package/src/reference-host.ts +359 -0
- package/src/runner.ts +189 -0
- package/src/scenario.ts +376 -0
- package/src/scenarios/delivery.ts +267 -0
- package/src/scenarios/index.ts +20 -0
- package/src/scenarios/interaction.ts +453 -0
- package/src/scenarios/ports.ts +278 -0
- package/src/scenarios/session.ts +183 -0
- package/src/scenarios/viewing.ts +184 -0
- package/src/version.ts +29 -0
- package/src/viewer.ts +380 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { Ports } from "@mostra/sdk";
|
|
3
|
+
//#region src/harness/protocol.d.ts
|
|
4
|
+
/** The harness protocol revisions this kit speaks, newest last. */
|
|
5
|
+
declare const HARNESS_REVISIONS: readonly string[];
|
|
6
|
+
/** The harness methods by name. */
|
|
7
|
+
declare const HARNESS_METHODS: {
|
|
8
|
+
readonly initialize: "harness/initialize";
|
|
9
|
+
readonly start: "harness/start";
|
|
10
|
+
readonly call: "harness/call";
|
|
11
|
+
readonly stop: "harness/stop";
|
|
12
|
+
readonly store: "harness/store";
|
|
13
|
+
readonly script: "harness/script";
|
|
14
|
+
readonly events: "harness/events";
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* What a host can do beyond the required methods: `attach` (`harness/start` with
|
|
18
|
+
* `mode: "attach"`), `faults` (`harness/script`, for reference and test hosts) and `events`
|
|
19
|
+
* (`harness/events`). Unknown features are ignored.
|
|
20
|
+
*/
|
|
21
|
+
type HarnessFeature = "attach" | "faults" | "events" | (string & {});
|
|
22
|
+
/** `harness/initialize`'s params. */
|
|
23
|
+
interface InitializeParams {
|
|
24
|
+
/** The harness protocol revision. */
|
|
25
|
+
readonly revision: string;
|
|
26
|
+
/** The kit that drives the host. */
|
|
27
|
+
readonly kit: {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly version: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** `harness/initialize`'s result. */
|
|
33
|
+
interface InitializeResult {
|
|
34
|
+
/** The harness protocol revision. */
|
|
35
|
+
readonly revision: string;
|
|
36
|
+
/** The host: its name and version. */
|
|
37
|
+
readonly host: {
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly version: string;
|
|
40
|
+
};
|
|
41
|
+
/** The ports the host registers in `mostra/initialize`. */
|
|
42
|
+
readonly ports: readonly string[];
|
|
43
|
+
/** The harness features the host supports. */
|
|
44
|
+
readonly features: readonly HarnessFeature[];
|
|
45
|
+
}
|
|
46
|
+
/** The schema of `InitializeResult`. */
|
|
47
|
+
declare const InitializeResult: z.ZodType<InitializeResult>;
|
|
48
|
+
/** `harness/start`'s params. */
|
|
49
|
+
interface StartParams {
|
|
50
|
+
/** The home the host must use: its own choice in production (HO-08), the kit's in a run. */
|
|
51
|
+
readonly home: string;
|
|
52
|
+
/** Variables the host adds to its instance's environment (test switches, fault points). */
|
|
53
|
+
readonly env: Readonly<Record<string, string>>;
|
|
54
|
+
/** Spawn an instance over stdio (default) or attach to the home's daemon (feature `attach`). */
|
|
55
|
+
readonly mode?: "spawn" | "attach";
|
|
56
|
+
}
|
|
57
|
+
/** The schema of `StartParams`. */
|
|
58
|
+
declare const StartParams: z.ZodType<StartParams>;
|
|
59
|
+
/** `harness/start`'s result: what `mostra/initialize` answered the host. */
|
|
60
|
+
interface StartResult {
|
|
61
|
+
/** The harness protocol revision. */
|
|
62
|
+
readonly revision: string;
|
|
63
|
+
/** The instance the host's session reached: its home, mode, PID, release and platform. */
|
|
64
|
+
readonly instance: {
|
|
65
|
+
readonly home: string;
|
|
66
|
+
readonly mode: string;
|
|
67
|
+
readonly pid: number;
|
|
68
|
+
readonly version: string;
|
|
69
|
+
readonly platform: string;
|
|
70
|
+
};
|
|
71
|
+
/** The session's warnings, such as an unsupported port. */
|
|
72
|
+
readonly warnings: readonly {
|
|
73
|
+
readonly code: string;
|
|
74
|
+
}[];
|
|
75
|
+
}
|
|
76
|
+
/** The schema of `StartResult`. */
|
|
77
|
+
declare const StartResult: z.ZodType<StartResult>;
|
|
78
|
+
/** `harness/call`'s params: one host-protocol request. */
|
|
79
|
+
interface CallParams {
|
|
80
|
+
/** The host-protocol method. */
|
|
81
|
+
readonly method: string;
|
|
82
|
+
/** Its params. */
|
|
83
|
+
readonly params: unknown;
|
|
84
|
+
/** How long the host waits for the answer, in milliseconds. */
|
|
85
|
+
readonly timeoutMs?: number;
|
|
86
|
+
}
|
|
87
|
+
/** The schema of `CallParams`. */
|
|
88
|
+
declare const CallParams: z.ZodType<CallParams>;
|
|
89
|
+
/** An instance's error as the host received it: the JSON-RPC code and the diagnostic. */
|
|
90
|
+
interface CallError {
|
|
91
|
+
/** The diagnostic code (`MOSTRA_…`). */
|
|
92
|
+
readonly code: number;
|
|
93
|
+
/** The diagnostic as it arrived. */
|
|
94
|
+
readonly data: {
|
|
95
|
+
readonly code: string;
|
|
96
|
+
readonly message?: string;
|
|
97
|
+
} & Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
/** `harness/call`'s result: the answer, or the instance's error as it arrived. */
|
|
100
|
+
type CallResult = {
|
|
101
|
+
readonly result: unknown;
|
|
102
|
+
} | {
|
|
103
|
+
readonly error: CallError;
|
|
104
|
+
};
|
|
105
|
+
/** The schema of `CallResult`. */
|
|
106
|
+
declare const CallResult: z.ZodType<CallResult>;
|
|
107
|
+
/** One delivery the host's submissions port received, as its store records it. */
|
|
108
|
+
interface StoredDelivery {
|
|
109
|
+
/** The delivery's stable ID (`dlv_…`). */
|
|
110
|
+
readonly deliveryId: string;
|
|
111
|
+
/** The submission it delivers. */
|
|
112
|
+
readonly submissionId: string;
|
|
113
|
+
/** The artifact. */
|
|
114
|
+
readonly artifactId: string;
|
|
115
|
+
/** How many times the port was called with this delivery. */
|
|
116
|
+
readonly calls: number;
|
|
117
|
+
/** How many copies the host's store holds: one for a conformant host (H-03). */
|
|
118
|
+
readonly stored: number;
|
|
119
|
+
/** The attempt numbers it saw, in order. */
|
|
120
|
+
readonly attempts: readonly number[];
|
|
121
|
+
/** `envelope.kind` as received. */
|
|
122
|
+
readonly kind: string;
|
|
123
|
+
/** `results.submission.channel`, `.viewer.kind` and `.viewer.ref`. */
|
|
124
|
+
readonly channel: string;
|
|
125
|
+
/** `results.submission.viewer.kind`. */
|
|
126
|
+
readonly viewerKind: string;
|
|
127
|
+
/** `results.submission.viewer.ref`, when the viewer has one. */
|
|
128
|
+
readonly viewerRef?: string;
|
|
129
|
+
/** The order the host stored it in, from 1. */
|
|
130
|
+
readonly order: number;
|
|
131
|
+
}
|
|
132
|
+
/** What the host's own store and port handlers saw. */
|
|
133
|
+
interface StoreResult {
|
|
134
|
+
/** The deliveries the host's store holds, in the order it stored them. */
|
|
135
|
+
readonly deliveries: readonly StoredDelivery[];
|
|
136
|
+
/** Draft mirrors, in arrival order. */
|
|
137
|
+
readonly drafts: readonly {
|
|
138
|
+
readonly artifactId: string;
|
|
139
|
+
readonly viewerId: string;
|
|
140
|
+
readonly revision: string;
|
|
141
|
+
readonly kind: string;
|
|
142
|
+
}[];
|
|
143
|
+
/** `trustedControls.activated` calls, in arrival order. */
|
|
144
|
+
readonly activations: readonly {
|
|
145
|
+
readonly artifactId: string;
|
|
146
|
+
readonly control: string;
|
|
147
|
+
readonly viewId: string;
|
|
148
|
+
}[];
|
|
149
|
+
/**
|
|
150
|
+
* `identity.resolve` calls: the artifact, how the request arrived, the header names, and what
|
|
151
|
+
* the host answered (absent when it failed or never answered). Scenarios judge the session by
|
|
152
|
+
* this answer, so a host may name people however it likes.
|
|
153
|
+
*/
|
|
154
|
+
readonly identity: readonly {
|
|
155
|
+
readonly artifactId: string;
|
|
156
|
+
readonly via: string;
|
|
157
|
+
readonly headers: readonly string[];
|
|
158
|
+
readonly answer?: {
|
|
159
|
+
readonly ref: string;
|
|
160
|
+
readonly name?: string;
|
|
161
|
+
} | null;
|
|
162
|
+
}[];
|
|
163
|
+
/**
|
|
164
|
+
* `trustedControls.policy` calls, with what the host answered (absent when it failed or never
|
|
165
|
+
* answered). Scenarios judge the view by this answer when they can't script one.
|
|
166
|
+
*/
|
|
167
|
+
readonly policies: readonly {
|
|
168
|
+
readonly artifactId: string;
|
|
169
|
+
readonly answer?: {
|
|
170
|
+
readonly submitChannels: readonly string[];
|
|
171
|
+
readonly controls?: readonly string[];
|
|
172
|
+
};
|
|
173
|
+
}[];
|
|
174
|
+
/** `secrets.get` calls. */
|
|
175
|
+
readonly secrets: readonly {
|
|
176
|
+
readonly artifactId: string;
|
|
177
|
+
readonly name: string;
|
|
178
|
+
}[];
|
|
179
|
+
}
|
|
180
|
+
/** The schema of `StoreResult`. */
|
|
181
|
+
declare const StoreResult: z.ZodType<StoreResult>;
|
|
182
|
+
/** How a scripted port answers from now on (feature `faults`). */
|
|
183
|
+
type ScriptParams = {
|
|
184
|
+
readonly port: "submissions";
|
|
185
|
+
readonly answer: "accept" | "retry" | "undeliverable" | "reject" | "fail" | "hang";
|
|
186
|
+
} | {
|
|
187
|
+
readonly port: "trustedControls";
|
|
188
|
+
readonly answer: "fail" | "hang" | {
|
|
189
|
+
readonly submitChannels: readonly string[];
|
|
190
|
+
readonly controls?: readonly string[];
|
|
191
|
+
};
|
|
192
|
+
} | {
|
|
193
|
+
readonly port: "identity";
|
|
194
|
+
/** `headers`: the person named by `x-forwarded-user`, else none. */
|
|
195
|
+
readonly answer: "fail" | "hang" | "headers" | null | {
|
|
196
|
+
readonly ref: string;
|
|
197
|
+
readonly name?: string;
|
|
198
|
+
};
|
|
199
|
+
} | {
|
|
200
|
+
readonly port: "secrets";
|
|
201
|
+
readonly answer: Readonly<Record<string, string>>;
|
|
202
|
+
};
|
|
203
|
+
/** The schema of `ScriptParams`. */
|
|
204
|
+
declare const ScriptParams: z.ZodType<ScriptParams>;
|
|
205
|
+
/** `harness/events`'s params: read pages from the host's own named stream (feature `events`). */
|
|
206
|
+
interface EventsParams {
|
|
207
|
+
/** The subscriber name: the stream resumes after what this name acknowledged. */
|
|
208
|
+
readonly name: string;
|
|
209
|
+
/** The events filter. */
|
|
210
|
+
readonly filter: {
|
|
211
|
+
readonly artifacts?: readonly string[];
|
|
212
|
+
readonly types?: readonly string[];
|
|
213
|
+
};
|
|
214
|
+
/** Stop after this many pages, or when `timeoutMs` passes. */
|
|
215
|
+
readonly pages: number;
|
|
216
|
+
/** How long the host waits for the answer, in milliseconds. */
|
|
217
|
+
readonly timeoutMs: number;
|
|
218
|
+
}
|
|
219
|
+
/** The schema of `EventsParams`. */
|
|
220
|
+
declare const EventsParams: z.ZodType<EventsParams>;
|
|
221
|
+
/** `harness/events`'s result: the pages read, every one handled and acknowledged. */
|
|
222
|
+
interface EventsResult {
|
|
223
|
+
/** The pages read, in cursor order. */
|
|
224
|
+
readonly pages: readonly {
|
|
225
|
+
readonly cursor: string;
|
|
226
|
+
readonly events: readonly ({
|
|
227
|
+
readonly cursor: string;
|
|
228
|
+
readonly type: string;
|
|
229
|
+
} & Record<string, unknown>)[];
|
|
230
|
+
}[];
|
|
231
|
+
}
|
|
232
|
+
/** The schema of `EventsResult`. */
|
|
233
|
+
declare const EventsResult: z.ZodType<EventsResult>;
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/reference-host.d.ts
|
|
236
|
+
/** The faults a broken reference host can carry. */
|
|
237
|
+
declare const REFERENCE_FAULTS: readonly string[];
|
|
238
|
+
/** The reference host's store and port answers: what a real host keeps in its own database. */
|
|
239
|
+
declare class ReferenceStore {
|
|
240
|
+
#private;
|
|
241
|
+
constructor(faults?: ReadonlySet<string>, unscripted?: boolean);
|
|
242
|
+
/** Sets how a port answers from now on (feature `faults`). */
|
|
243
|
+
script(params: ScriptParams): void;
|
|
244
|
+
/** The ports, as the SDK takes them. */
|
|
245
|
+
ports(): Ports;
|
|
246
|
+
/** What the store holds now. */
|
|
247
|
+
snapshot(): StoreResult;
|
|
248
|
+
}
|
|
249
|
+
/** Runs the reference host's harness on stdio. */
|
|
250
|
+
declare function runReferenceHost(options?: {
|
|
251
|
+
standalone?: boolean;
|
|
252
|
+
unscripted?: boolean;
|
|
253
|
+
faults?: readonly string[];
|
|
254
|
+
}): Promise<void>;
|
|
255
|
+
//#endregion
|
|
256
|
+
export { StoreResult as _, CallParams as a, EventsResult as c, HarnessFeature as d, InitializeParams as f, StartResult as g, StartParams as h, CallError as i, HARNESS_METHODS as l, ScriptParams as m, ReferenceStore as n, CallResult as o, InitializeResult as p, runReferenceHost as r, EventsParams as s, REFERENCE_FAULTS as t, HARNESS_REVISIONS as u, StoredDelivery as v };
|
|
257
|
+
//# sourceMappingURL=reference-host-BJ-A_Qd_.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference-host-BJ-A_Qd_.d.ts","names":[],"sources":["../src/harness/protocol.ts","../src/reference-host.ts"],"mappings":";;;;cAsBa;;cAGA;WACF;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;KAgBC;;UAGK;;WAEN;;WAEA;aAAgB;aAAuB;;;;UAIjC;;WAEN;;WAEA;aAAiB;aAAuB;;;WAExC;;WAEA,mBAAmB;;;cAIjB,kBAAkB,EAAE,QAAQ;;UAQxB;;WAEN;;WAEA,KAAK,SAAS;;WAEd;;;cAIE,aAAa,EAAE,QAAQ;;UAOnB;;WAEN;;WAEA;aACE;aACA;aACA;aACA;aACA;;;WAGF;aAA8B;;;;cAI5B,aAAa,EAAE,QAAQ;;UAanB;;WAEN;;WAEA;;WAEA;;;cAIE,YAAY,EAAE,QAAQ;;UAOlB;;WAEN;;WAEA;aAAiB;aAAuB;MAAqB;;;KAI5D;WAAwB;;WAA+B,OAAO;;;cAG7D,YAAY,EAAE,QAAQ;;UAMlB;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;;UAIM;;WAEN,qBAAqB;;WAErB;aACE;aACA;aACA;aACA;;;WAGF;aACE;aACA;aACA;;;;;;;WAOF;aACE;aACA;aACA;aACA;eAAoB;eAAsB;;;;;;;WAM5C;aACE;aACA;eACE;eACA;;;;WAIJ;aAA6B;aAA6B;;;;cAIxD,aAAa,EAAE,QAAQ;;KAoDxB;WAEG;WACA;;WAGA;WACA;aAIM;aACA;;;WAIN;;WAEA;aAKM;aAAsB;;;WAE5B;WAA0B,QAAQ,SAAS;;;cAG7C,cAAc,EAAE,QAAQ;;UA8BpB;;WAEN;;WAEA;aAAmB;aAAwC;;;WAE3D;;WAEA;;;cAIE,cAAc,EAAE,QAAQ;;UAWpB;;WAEN;aACE;aACA;eAA6B;eAAyB;QAAiB;;;;cAQvE,cAAc,EAAE,QAAQ;;;;cCrUxB;;cAOA;;EAsBX,YAAY,SAAQ,qBAAiC;;EAOrD,OAAO,QAAQ;;EAyCf,SAAS;;EAuFT,YAAY;;;iBAaQ,iBACpB;EAAW;EAAsB;EAAsB;IACtD"}
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { a as HARNESS_METHODS, c as ScriptParams, f as kitVersion, l as StartParams, o as HARNESS_REVISIONS, r as EventsParams, t as CallParams } from "./protocol-RQ9lbehE.js";
|
|
2
|
+
import { LineSplitter, RpcError, RpcPeer, createDiagnostic, encodeLine, hostMethods } from "@mostra/protocol";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { createMostra } from "@mostra/sdk";
|
|
5
|
+
//#region src/harness/serve.ts
|
|
6
|
+
/**
|
|
7
|
+
* The host's side of the harness protocol, for hosts written in TypeScript: `serveHarness` reads
|
|
8
|
+
* the kit's requests on stdin and answers them with the host's implementation, on stdout. A host
|
|
9
|
+
* writes its shim as one small program around its real integration:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* serveHarness({
|
|
13
|
+
* initialize: () => ({ host: { name: "my-host", version: "1.0.0" }, ports: ["submissions"], features: [] }),
|
|
14
|
+
* start: (params) => myHost.startMostra(params.home, params.env),
|
|
15
|
+
* call: (method, params) => myHost.mostra.call(method, params),
|
|
16
|
+
* stop: () => myHost.stopMostra(),
|
|
17
|
+
* store: () => myHost.conformanceStore(),
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* Nothing but protocol messages may go to stdout; log to stderr.
|
|
22
|
+
*/
|
|
23
|
+
function invalid(method, message) {
|
|
24
|
+
return new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message: `${method}: ${message}` }), -32602);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Serves the harness protocol on this process's stdin and stdout until stdin ends, then stops
|
|
28
|
+
* the host's session and resolves.
|
|
29
|
+
*/
|
|
30
|
+
function serveHarness(implementation, io = {}) {
|
|
31
|
+
const input = io.input ?? process.stdin;
|
|
32
|
+
const output = io.output ?? process.stdout;
|
|
33
|
+
const peer = new RpcPeer({
|
|
34
|
+
send: (message) => {
|
|
35
|
+
output.write(encodeLine(message));
|
|
36
|
+
},
|
|
37
|
+
onRequest: async (method, params) => {
|
|
38
|
+
switch (method) {
|
|
39
|
+
case HARNESS_METHODS.initialize: return {
|
|
40
|
+
revision: HARNESS_REVISIONS.at(-1),
|
|
41
|
+
...await implementation.initialize()
|
|
42
|
+
};
|
|
43
|
+
case HARNESS_METHODS.start: {
|
|
44
|
+
const parsed = StartParams.safeParse(params);
|
|
45
|
+
if (!parsed.success) throw invalid(method, parsed.error.message);
|
|
46
|
+
return await implementation.start(parsed.data);
|
|
47
|
+
}
|
|
48
|
+
case HARNESS_METHODS.call: {
|
|
49
|
+
const parsed = CallParams.safeParse(params);
|
|
50
|
+
if (!parsed.success) throw invalid(method, parsed.error.message);
|
|
51
|
+
try {
|
|
52
|
+
return { result: await implementation.call(parsed.data.method, parsed.data.params, parsed.data.timeoutMs) ?? null };
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error instanceof RpcError) return { error: {
|
|
55
|
+
code: error.code,
|
|
56
|
+
data: { ...error.diagnostic }
|
|
57
|
+
} };
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
case HARNESS_METHODS.stop:
|
|
62
|
+
await implementation.stop();
|
|
63
|
+
return {};
|
|
64
|
+
case HARNESS_METHODS.store: return await implementation.store();
|
|
65
|
+
case HARNESS_METHODS.script: {
|
|
66
|
+
if (!implementation.script) break;
|
|
67
|
+
const parsed = ScriptParams.safeParse(params);
|
|
68
|
+
if (!parsed.success) throw invalid(method, parsed.error.message);
|
|
69
|
+
await implementation.script(parsed.data);
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
case HARNESS_METHODS.events: {
|
|
73
|
+
if (!implementation.events) break;
|
|
74
|
+
const parsed = EventsParams.safeParse(params);
|
|
75
|
+
if (!parsed.success) throw invalid(method, parsed.error.message);
|
|
76
|
+
return await implementation.events(parsed.data);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
throw new RpcError(createDiagnostic("MOSTRA_HOST_METHOD_UNKNOWN", { message: `This host serves no harness method ${method}.` }), -32601);
|
|
80
|
+
},
|
|
81
|
+
onInternalError: (error, method) => {
|
|
82
|
+
process.stderr.write(`harness: ${method} failed: ${error.stack ?? error}\n`);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
const lines = new LineSplitter();
|
|
86
|
+
input.setEncoding?.("utf8");
|
|
87
|
+
input.on("data", (chunk) => {
|
|
88
|
+
for (const line of lines.push(chunk)) peer.receiveLine(line);
|
|
89
|
+
});
|
|
90
|
+
return new Promise((done) => {
|
|
91
|
+
input.on("end", () => {
|
|
92
|
+
Promise.resolve(implementation.stop()).catch(() => void 0).finally(() => {
|
|
93
|
+
peer.close();
|
|
94
|
+
done();
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/reference-host.ts
|
|
101
|
+
/**
|
|
102
|
+
* `mostra-conformance-reference-host`: the TypeScript reference host (HO-05). A host built the way
|
|
103
|
+
* the docs recommend, on `@mostra/sdk`, with a store that keeps each submission once by its
|
|
104
|
+
* `deliveryId` (H-03), every port the SDK serves, and the harness shim the kit drives. It is the
|
|
105
|
+
* model a TypeScript host copies, and the kit's own proof that its scenarios pass.
|
|
106
|
+
*
|
|
107
|
+
* mostra-conformance-reference-host every port
|
|
108
|
+
* mostra-conformance-reference-host --standalone no port: Mostra's built-in adapters
|
|
109
|
+
* mostra-conformance-reference-host --unscripted every port, without the `faults` feature:
|
|
110
|
+
* its policy allows both channels and it names people by its own IDs (`user-1` …), so the
|
|
111
|
+
* kit judges it by the answers it reports, as it must any host that can't be scripted
|
|
112
|
+
* mostra-conformance-reference-host --break <fault,…> a deliberately broken host:
|
|
113
|
+
* store-duplicates stores every delivery call, so a redelivery is stored twice
|
|
114
|
+
* drafts-as-submissions takes draft mirrors for submissions
|
|
115
|
+
* ignore-policy allows both channels whatever the kit scripted
|
|
116
|
+
*
|
|
117
|
+
* Its own runtime is found like any SDK host's (`MOSTRA_BUNDLE`, `MOSTRA_RUNTIME_BIN`, the pinned
|
|
118
|
+
* bundle, a development checkout).
|
|
119
|
+
*/
|
|
120
|
+
/** The faults a broken reference host can carry. */
|
|
121
|
+
const REFERENCE_FAULTS = [
|
|
122
|
+
"store-duplicates",
|
|
123
|
+
"drafts-as-submissions",
|
|
124
|
+
"ignore-policy"
|
|
125
|
+
];
|
|
126
|
+
/** The reference host's store and port answers: what a real host keeps in its own database. */
|
|
127
|
+
var ReferenceStore = class {
|
|
128
|
+
#deliveries = /* @__PURE__ */ new Map();
|
|
129
|
+
#drafts = [];
|
|
130
|
+
#activations = [];
|
|
131
|
+
#identity = [];
|
|
132
|
+
#policies = [];
|
|
133
|
+
#secrets = [];
|
|
134
|
+
#faults;
|
|
135
|
+
#order = 0;
|
|
136
|
+
/** How the scripted ports answer (feature `faults`). */
|
|
137
|
+
#deliver = "accept";
|
|
138
|
+
#policy = {
|
|
139
|
+
submitChannels: ["shell"],
|
|
140
|
+
controls: [
|
|
141
|
+
"submit",
|
|
142
|
+
"choose",
|
|
143
|
+
"approve"
|
|
144
|
+
]
|
|
145
|
+
};
|
|
146
|
+
#person = "headers";
|
|
147
|
+
#secretValues = {};
|
|
148
|
+
/** People by the header that named them, for an unscripted host's own IDs. */
|
|
149
|
+
#people = /* @__PURE__ */ new Map();
|
|
150
|
+
#unscripted;
|
|
151
|
+
constructor(faults = /* @__PURE__ */ new Set(), unscripted = false) {
|
|
152
|
+
this.#faults = faults;
|
|
153
|
+
this.#unscripted = unscripted;
|
|
154
|
+
if (unscripted) this.#policy = { submitChannels: ["artifact", "shell"] };
|
|
155
|
+
}
|
|
156
|
+
/** Sets how a port answers from now on (feature `faults`). */
|
|
157
|
+
script(params) {
|
|
158
|
+
if (params.port === "submissions") this.#deliver = params.answer;
|
|
159
|
+
else if (params.port === "trustedControls") this.#policy = params.answer;
|
|
160
|
+
else if (params.port === "identity") this.#person = params.answer;
|
|
161
|
+
else this.#secretValues = params.answer;
|
|
162
|
+
}
|
|
163
|
+
/** Records one delivery the way a host's database would: once per `deliveryId`. */
|
|
164
|
+
#record(envelope) {
|
|
165
|
+
const id = "deliveryId" in envelope ? envelope.deliveryId : `draft-${envelope.viewerId}-${envelope.revision}`;
|
|
166
|
+
const existing = this.#deliveries.get(id);
|
|
167
|
+
const attempt = "attempt" in envelope ? envelope.attempt : 1;
|
|
168
|
+
if (existing) {
|
|
169
|
+
existing.attempts.push(attempt);
|
|
170
|
+
existing.calls += 1;
|
|
171
|
+
if (this.#faults.has("store-duplicates")) existing.stored += 1;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const results = "results" in envelope ? envelope.results : void 0;
|
|
175
|
+
this.#order += 1;
|
|
176
|
+
this.#deliveries.set(id, {
|
|
177
|
+
deliveryId: id,
|
|
178
|
+
submissionId: results?.submission.id ?? "",
|
|
179
|
+
artifactId: envelope.artifactId,
|
|
180
|
+
calls: 1,
|
|
181
|
+
stored: 1,
|
|
182
|
+
attempts: [attempt],
|
|
183
|
+
kind: envelope.kind,
|
|
184
|
+
channel: results?.submission.channel ?? "",
|
|
185
|
+
viewerKind: results?.submission.viewer.kind ?? "",
|
|
186
|
+
...results?.submission.viewer.ref !== void 0 ? { viewerRef: results.submission.viewer.ref } : {},
|
|
187
|
+
order: this.#order
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
/** The ports, as the SDK takes them. */
|
|
191
|
+
ports() {
|
|
192
|
+
return {
|
|
193
|
+
submissions: { deliver: async (envelope) => {
|
|
194
|
+
const answer = this.#deliver;
|
|
195
|
+
if (answer === "fail") throw new Error("the host's store is unavailable");
|
|
196
|
+
if (answer === "hang") return new Promise(() => void 0);
|
|
197
|
+
if (answer === "retry") return {
|
|
198
|
+
accepted: false,
|
|
199
|
+
reason: "busy",
|
|
200
|
+
retry: true
|
|
201
|
+
};
|
|
202
|
+
if (answer === "undeliverable") return {
|
|
203
|
+
accepted: false,
|
|
204
|
+
reason: "undeliverable",
|
|
205
|
+
retry: false
|
|
206
|
+
};
|
|
207
|
+
if (answer === "reject") return {
|
|
208
|
+
accepted: false,
|
|
209
|
+
reason: "invalid",
|
|
210
|
+
retry: false
|
|
211
|
+
};
|
|
212
|
+
this.#record(envelope);
|
|
213
|
+
return { accepted: true };
|
|
214
|
+
} },
|
|
215
|
+
drafts: { mirror: (draft) => {
|
|
216
|
+
this.#drafts.push({
|
|
217
|
+
artifactId: draft.artifactId,
|
|
218
|
+
viewerId: draft.viewerId,
|
|
219
|
+
revision: draft.revision,
|
|
220
|
+
kind: draft.kind
|
|
221
|
+
});
|
|
222
|
+
if (this.#faults.has("drafts-as-submissions")) this.#record(draft);
|
|
223
|
+
} },
|
|
224
|
+
identity: { resolve: async (request) => {
|
|
225
|
+
const call = {
|
|
226
|
+
artifactId: request.artifactId,
|
|
227
|
+
via: request.via,
|
|
228
|
+
headers: Object.keys(request.headers).sort()
|
|
229
|
+
};
|
|
230
|
+
const answer = this.#person;
|
|
231
|
+
if (answer === "fail" || answer === "hang") {
|
|
232
|
+
this.#identity.push(call);
|
|
233
|
+
if (answer === "fail") throw new Error("the host's directory is unavailable");
|
|
234
|
+
return new Promise(() => void 0);
|
|
235
|
+
}
|
|
236
|
+
let person = answer === "headers" ? null : answer;
|
|
237
|
+
if (answer === "headers") {
|
|
238
|
+
const user = request.headers["x-forwarded-user"];
|
|
239
|
+
if (user && this.#unscripted && !this.#people.has(user)) this.#people.set(user, `user-${this.#people.size + 1}`);
|
|
240
|
+
const ref = user && this.#unscripted ? this.#people.get(user) : user;
|
|
241
|
+
person = user && ref ? {
|
|
242
|
+
ref,
|
|
243
|
+
name: user.split("@")[0] ?? user
|
|
244
|
+
} : null;
|
|
245
|
+
}
|
|
246
|
+
this.#identity.push({
|
|
247
|
+
...call,
|
|
248
|
+
answer: person
|
|
249
|
+
});
|
|
250
|
+
return person;
|
|
251
|
+
} },
|
|
252
|
+
trustedControls: {
|
|
253
|
+
policy: async (artifact) => {
|
|
254
|
+
const answer = this.#policy;
|
|
255
|
+
if (this.#faults.has("ignore-policy")) {
|
|
256
|
+
const both = { submitChannels: ["shell", "artifact"] };
|
|
257
|
+
this.#policies.push({
|
|
258
|
+
artifactId: artifact.id,
|
|
259
|
+
answer: both
|
|
260
|
+
});
|
|
261
|
+
return both;
|
|
262
|
+
}
|
|
263
|
+
if (answer === "fail" || answer === "hang") {
|
|
264
|
+
this.#policies.push({ artifactId: artifact.id });
|
|
265
|
+
if (answer === "fail") throw new Error("the host's policy service is unavailable");
|
|
266
|
+
return new Promise(() => void 0);
|
|
267
|
+
}
|
|
268
|
+
this.#policies.push({
|
|
269
|
+
artifactId: artifact.id,
|
|
270
|
+
answer
|
|
271
|
+
});
|
|
272
|
+
return answer;
|
|
273
|
+
},
|
|
274
|
+
activated: (event) => {
|
|
275
|
+
this.#activations.push({
|
|
276
|
+
artifactId: event.artifactId,
|
|
277
|
+
control: event.control,
|
|
278
|
+
viewId: event.viewId
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
secrets: { get: (request) => {
|
|
283
|
+
this.#secrets.push({
|
|
284
|
+
artifactId: request.artifactId,
|
|
285
|
+
name: request.name
|
|
286
|
+
});
|
|
287
|
+
return this.#secretValues[request.name] ?? null;
|
|
288
|
+
} }
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
/** What the store holds now. */
|
|
292
|
+
snapshot() {
|
|
293
|
+
return {
|
|
294
|
+
deliveries: [...this.#deliveries.values()].sort((a, b) => a.order - b.order),
|
|
295
|
+
drafts: [...this.#drafts],
|
|
296
|
+
activations: [...this.#activations],
|
|
297
|
+
identity: [...this.#identity],
|
|
298
|
+
policies: [...this.#policies],
|
|
299
|
+
secrets: [...this.#secrets]
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
/** Runs the reference host's harness on stdio. */
|
|
304
|
+
async function runReferenceHost(options = {}) {
|
|
305
|
+
const faults = new Set(options.faults ?? []);
|
|
306
|
+
const unscripted = options.unscripted ?? false;
|
|
307
|
+
const store = new ReferenceStore(faults, unscripted);
|
|
308
|
+
let mostra;
|
|
309
|
+
const ports = options.standalone ? {} : store.ports();
|
|
310
|
+
const portNames = Object.keys(ports);
|
|
311
|
+
const name = options.standalone ? "reference-host-ts-standalone" : unscripted ? "reference-host-ts-unscripted" : faults.size > 0 ? "reference-host-ts-broken" : "reference-host-ts";
|
|
312
|
+
await serveHarness({
|
|
313
|
+
initialize: () => ({
|
|
314
|
+
host: {
|
|
315
|
+
name,
|
|
316
|
+
version: kitVersion()
|
|
317
|
+
},
|
|
318
|
+
ports: portNames,
|
|
319
|
+
features: options.standalone || unscripted ? ["events"] : ["faults", "events"]
|
|
320
|
+
}),
|
|
321
|
+
async start(params) {
|
|
322
|
+
if (params.mode === "attach") throw new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message: "The TypeScript reference host spawns its instance; it doesn't attach." }));
|
|
323
|
+
await mostra?.close();
|
|
324
|
+
mostra = await createMostra({
|
|
325
|
+
home: params.home,
|
|
326
|
+
client: {
|
|
327
|
+
name,
|
|
328
|
+
version: kitVersion()
|
|
329
|
+
},
|
|
330
|
+
ports,
|
|
331
|
+
env: {
|
|
332
|
+
...process.env,
|
|
333
|
+
...params.env
|
|
334
|
+
},
|
|
335
|
+
onLog: () => void 0
|
|
336
|
+
});
|
|
337
|
+
return {
|
|
338
|
+
revision: mostra.revision,
|
|
339
|
+
instance: mostra.instance,
|
|
340
|
+
warnings: mostra.warnings
|
|
341
|
+
};
|
|
342
|
+
},
|
|
343
|
+
async call(method, params, timeoutMs) {
|
|
344
|
+
const definition = hostMethods.find((candidate) => candidate.method === method);
|
|
345
|
+
if (!mostra) throw new RpcError(createDiagnostic("MOSTRA_HOST_CONNECTION_CLOSED", { message: "No instance runs." }));
|
|
346
|
+
if (!definition) throw new RpcError(createDiagnostic("MOSTRA_HOST_METHOD_UNKNOWN", { message: `No method ${method}.` }), -32601);
|
|
347
|
+
return mostra.call(definition, params, timeoutMs !== void 0 ? { timeoutMs } : {});
|
|
348
|
+
},
|
|
349
|
+
async stop() {
|
|
350
|
+
const current = mostra;
|
|
351
|
+
mostra = void 0;
|
|
352
|
+
await current?.close();
|
|
353
|
+
},
|
|
354
|
+
store: () => store.snapshot(),
|
|
355
|
+
...options.standalone || unscripted ? {} : { script: (params) => store.script(params) },
|
|
356
|
+
async events(params) {
|
|
357
|
+
if (!mostra) throw new Error("No instance runs.");
|
|
358
|
+
const pages = [];
|
|
359
|
+
const stream = mostra.events({
|
|
360
|
+
name: params.name,
|
|
361
|
+
filter: {
|
|
362
|
+
...params.filter.artifacts ? { artifacts: [...params.filter.artifacts] } : {},
|
|
363
|
+
...params.filter.types ? { types: [...params.filter.types] } : {}
|
|
364
|
+
}
|
|
365
|
+
})[Symbol.asyncIterator]();
|
|
366
|
+
const deadline = Date.now() + params.timeoutMs;
|
|
367
|
+
try {
|
|
368
|
+
while (pages.length < params.pages) {
|
|
369
|
+
const left = deadline - Date.now();
|
|
370
|
+
if (left <= 0) break;
|
|
371
|
+
const next = await Promise.race([stream.next(), new Promise((done) => setTimeout(() => done("timeout"), left))]);
|
|
372
|
+
if (next === "timeout" || next.done) break;
|
|
373
|
+
pages.push({
|
|
374
|
+
cursor: next.value.cursor,
|
|
375
|
+
events: next.value.events.map((event) => ({ ...event }))
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
} finally {
|
|
379
|
+
await Promise.race([stream.next().catch(() => void 0), new Promise((done) => setTimeout(done, 1e3))]);
|
|
380
|
+
await stream.return?.();
|
|
381
|
+
}
|
|
382
|
+
return { pages };
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
if (import.meta.url === `file://${process.argv[1]}` || process.argv[1]?.endsWith("reference-host.js")) {
|
|
387
|
+
const { values } = parseArgs({
|
|
388
|
+
options: {
|
|
389
|
+
standalone: { type: "boolean" },
|
|
390
|
+
unscripted: { type: "boolean" },
|
|
391
|
+
break: { type: "string" }
|
|
392
|
+
},
|
|
393
|
+
strict: true
|
|
394
|
+
});
|
|
395
|
+
const faults = values.break ? values.break.split(",").filter(Boolean) : [];
|
|
396
|
+
for (const fault of faults) if (!REFERENCE_FAULTS.includes(fault)) {
|
|
397
|
+
process.stderr.write(`Unknown fault ${fault}; known: ${REFERENCE_FAULTS.join(", ")}\n`);
|
|
398
|
+
process.exit(2);
|
|
399
|
+
}
|
|
400
|
+
runReferenceHost({
|
|
401
|
+
standalone: values.standalone ?? false,
|
|
402
|
+
unscripted: values.unscripted ?? false,
|
|
403
|
+
faults
|
|
404
|
+
}).then(() => process.exit(0));
|
|
405
|
+
}
|
|
406
|
+
//#endregion
|
|
407
|
+
export { serveHarness as i, ReferenceStore as n, runReferenceHost as r, REFERENCE_FAULTS as t };
|
|
408
|
+
|
|
409
|
+
//# sourceMappingURL=reference-host-d6gs3Tal.js.map
|