@mostra/protocol 0.4.0 → 0.6.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/bundle.d.ts +14 -3
- package/dist/bundle.d.ts.map +1 -1
- package/dist/bundle.js +18 -6
- package/dist/bundle.js.map +1 -1
- package/dist/diagnostics/codes/host-core.d.ts +1 -0
- package/dist/diagnostics/codes/host-core.js +68 -0
- package/dist/diagnostics/codes/host-core.js.map +1 -0
- package/dist/diagnostics/codes/host.d.ts.map +1 -1
- package/dist/diagnostics/codes/host.js +6 -61
- package/dist/diagnostics/codes/host.js.map +1 -1
- package/dist/diagnostics/codes/index.js +1 -1
- package/dist/diagnostics/codes/platform.js +3 -3
- package/dist/diagnostics/codes/platform.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +4 -3
- package/dist/receive.d.ts +24 -0
- package/dist/receive.d.ts.map +1 -0
- package/dist/receive.js +137 -0
- package/dist/receive.js.map +1 -0
- package/dist/rpc/error.d.ts +1 -1
- package/dist/rpc/error.d.ts.map +1 -1
- package/dist/rpc/error.js +4 -4
- package/dist/rpc/error.js.map +1 -1
- package/dist/rpc/peer.d.ts +3 -1
- package/dist/rpc/peer.d.ts.map +1 -1
- package/dist/rpc/peer.js +5 -2
- package/dist/rpc/peer.js.map +1 -1
- package/package.json +4 -4
- package/schemas/protocol.draft-07.json +8 -2
- package/schemas/protocol.schema.json +8 -2
- package/src/bundle.ts +35 -6
- package/src/diagnostics/codes/host-core.ts +87 -0
- package/src/diagnostics/codes/host.ts +6 -67
- package/src/diagnostics/codes/platform.ts +4 -4
- package/src/index.ts +2 -0
- package/src/receive.ts +176 -0
- package/src/rpc/error.ts +8 -5
- package/src/rpc/peer.ts +5 -2
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HOST-area codes the JSON-RPC core raises itself (D83): malformed messages, unknown methods,
|
|
3
|
+
* invalid params, internal errors, closed connections and timeouts. They are defined here once
|
|
4
|
+
* and spread into the HOST catalog, so a browser bundle that imports the core (the bridge,
|
|
5
|
+
* `@mostra/react`, the shell) carries these six entries, not the whole area's text: a new HOST
|
|
6
|
+
* code no longer grows them (#17).
|
|
7
|
+
*/
|
|
8
|
+
import type { AreaCatalog } from "../spec.ts";
|
|
9
|
+
|
|
10
|
+
/** The codes the JSON-RPC core raises. */
|
|
11
|
+
export type CoreHostDiagnosticCode =
|
|
12
|
+
| "MOSTRA_HOST_MALFORMED"
|
|
13
|
+
| "MOSTRA_HOST_METHOD_UNKNOWN"
|
|
14
|
+
| "MOSTRA_HOST_INVALID_PARAMS"
|
|
15
|
+
| "MOSTRA_HOST_INTERNAL_ERROR"
|
|
16
|
+
| "MOSTRA_HOST_CONNECTION_CLOSED"
|
|
17
|
+
| "MOSTRA_HOST_TIMEOUT";
|
|
18
|
+
|
|
19
|
+
/** Their catalog entries. */
|
|
20
|
+
export const coreHostDiagnostics: AreaCatalog<CoreHostDiagnosticCode> = {
|
|
21
|
+
MOSTRA_HOST_MALFORMED: {
|
|
22
|
+
severity: "error",
|
|
23
|
+
phase: "host",
|
|
24
|
+
title: "Malformed protocol message",
|
|
25
|
+
hint: "Send each message as one line of JSON holding a single JSON-RPC 2.0 request or notification.",
|
|
26
|
+
docs: {
|
|
27
|
+
explanation:
|
|
28
|
+
"Every message on a Mostra connection is one line of JSON with one JSON-RPC 2.0 object. This code means a line wasn't valid JSON, wasn't a JSON-RPC 2.0 request or response, or was a batch (a JSON array), which revision `2026-10-10` doesn't accept. A client also reports it when a response doesn't match the method's result schema.",
|
|
29
|
+
fix: 'Encode one request per line with `"jsonrpc": "2.0"`, a `method`, an `id` for requests, and `params` as an object. Send several requests as separate lines instead of a batch.',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
MOSTRA_HOST_METHOD_UNKNOWN: {
|
|
33
|
+
severity: "error",
|
|
34
|
+
phase: "host",
|
|
35
|
+
title: "Unknown protocol method",
|
|
36
|
+
hint: "Call only the methods listed in `capabilities.methods` of the `mostra/initialize` result.",
|
|
37
|
+
docs: {
|
|
38
|
+
explanation:
|
|
39
|
+
"The instance doesn't serve the method that was called. Either the name is misspelled, or the client is newer than the instance and calls a method the instance's release doesn't have.",
|
|
40
|
+
fix: "Check the method name against INTERFACES \u00a76, and against `capabilities.methods` in the `mostra/initialize` result, which lists every method this instance serves. Upgrade the instance if the client needs a newer method.",
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
MOSTRA_HOST_INVALID_PARAMS: {
|
|
44
|
+
severity: "error",
|
|
45
|
+
phase: "host",
|
|
46
|
+
title: "Invalid method params",
|
|
47
|
+
hint: "Fix the params named in the message so they match the method's schema.",
|
|
48
|
+
docs: {
|
|
49
|
+
explanation:
|
|
50
|
+
"The request's `params` don't match the method's published schema: a required field is missing, a value has the wrong type, or a value is outside its allowed range. The message names the first problem found.",
|
|
51
|
+
fix: "Compare the request with the method's schema in `@mostra/protocol` (or the published JSON Schema) and correct the field named in the message.",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
MOSTRA_HOST_INTERNAL_ERROR: {
|
|
55
|
+
severity: "error",
|
|
56
|
+
phase: "host",
|
|
57
|
+
title: "Unexpected failure in the instance",
|
|
58
|
+
hint: "Retry the request with the same request ID; if it fails again, report it with the instance's log.",
|
|
59
|
+
docs: {
|
|
60
|
+
explanation:
|
|
61
|
+
"The instance failed while handling the request, for a reason that isn't one of its known conditions. Durable state is unaffected: a request is acknowledged only after its ledger transaction commits. The details are in the instance's log (`logs/supervisor.log` in the home), not in the response.",
|
|
62
|
+
fix: "Retry the request with the same request ID; retries are idempotent. If the failure repeats, report it with the relevant lines of `logs/supervisor.log`.",
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
MOSTRA_HOST_CONNECTION_CLOSED: {
|
|
66
|
+
severity: "error",
|
|
67
|
+
phase: "host",
|
|
68
|
+
title: "Connection closed",
|
|
69
|
+
hint: "Reconnect, then retry the request with the same request ID.",
|
|
70
|
+
docs: {
|
|
71
|
+
explanation:
|
|
72
|
+
"The connection to the other side closed before a response arrived, for example because the instance exited or restarted. The request may or may not have taken effect.",
|
|
73
|
+
fix: "Reconnect and send `mostra/initialize` again, then retry the request with the same request ID. Mutating methods are idempotent by request ID, so a retry never applies twice.",
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
MOSTRA_HOST_TIMEOUT: {
|
|
77
|
+
severity: "error",
|
|
78
|
+
phase: "host",
|
|
79
|
+
title: "Request timed out",
|
|
80
|
+
hint: "Retry the request with the same request ID, or allow it more time.",
|
|
81
|
+
docs: {
|
|
82
|
+
explanation:
|
|
83
|
+
"No response arrived within the time the caller allowed. The other side may still be working on the request, and it may still take effect.",
|
|
84
|
+
fix: "Retry with the same request ID, which returns the recorded result if the first attempt completed. Allow a longer timeout for slow operations.",
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
};
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Diagnostic codes of the HOST area: the host protocol between clients and an instance.
|
|
3
3
|
*/
|
|
4
4
|
import type { AreaCatalog } from "../spec.ts";
|
|
5
|
+
import { coreHostDiagnostics } from "./host-core.ts";
|
|
5
6
|
|
|
6
7
|
/** The codes of the HOST area, in the order they were added. */
|
|
7
8
|
export const hostDiagnosticCodes = [
|
|
@@ -27,8 +28,12 @@ export const hostDiagnosticCodes = [
|
|
|
27
28
|
/** The codes of the HOST area. */
|
|
28
29
|
export type HostDiagnosticCode = (typeof hostDiagnosticCodes)[number];
|
|
29
30
|
|
|
30
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* The HOST area's catalog entries. The codes the JSON-RPC core raises live in `host-core.ts`, so
|
|
33
|
+
* browser bundles carry only theirs (#17).
|
|
34
|
+
*/
|
|
31
35
|
export const hostDiagnostics: AreaCatalog<HostDiagnosticCode> = {
|
|
36
|
+
...coreHostDiagnostics,
|
|
32
37
|
MOSTRA_HOST_REVISION_UNSUPPORTED: {
|
|
33
38
|
severity: "error",
|
|
34
39
|
phase: "host",
|
|
@@ -40,50 +45,6 @@ export const hostDiagnostics: AreaCatalog<HostDiagnosticCode> = {
|
|
|
40
45
|
fix: "Restart the daemon so it runs the same release as the client: `mostra daemon restart`. Durable state survives the restart, and open views reconnect. A host that embeds Mostra updates its pinned Mostra runtime or its host library until both support a common revision.",
|
|
41
46
|
},
|
|
42
47
|
},
|
|
43
|
-
MOSTRA_HOST_MALFORMED: {
|
|
44
|
-
severity: "error",
|
|
45
|
-
phase: "host",
|
|
46
|
-
title: "Malformed protocol message",
|
|
47
|
-
hint: "Send each message as one line of JSON holding a single JSON-RPC 2.0 request or notification.",
|
|
48
|
-
docs: {
|
|
49
|
-
explanation:
|
|
50
|
-
"Every message on a Mostra connection is one line of JSON with one JSON-RPC 2.0 object. This code means a line wasn't valid JSON, wasn't a JSON-RPC 2.0 request or response, or was a batch (a JSON array), which revision `2026-10-10` doesn't accept. A client also reports it when a response doesn't match the method's result schema.",
|
|
51
|
-
fix: 'Encode one request per line with `"jsonrpc": "2.0"`, a `method`, an `id` for requests, and `params` as an object. Send several requests as separate lines instead of a batch.',
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
MOSTRA_HOST_METHOD_UNKNOWN: {
|
|
55
|
-
severity: "error",
|
|
56
|
-
phase: "host",
|
|
57
|
-
title: "Unknown protocol method",
|
|
58
|
-
hint: "Call only the methods listed in `capabilities.methods` of the `mostra/initialize` result.",
|
|
59
|
-
docs: {
|
|
60
|
-
explanation:
|
|
61
|
-
"The instance doesn't serve the method that was called. Either the name is misspelled, or the client is newer than the instance and calls a method the instance's release doesn't have.",
|
|
62
|
-
fix: "Check the method name against INTERFACES \u00a76, and against `capabilities.methods` in the `mostra/initialize` result, which lists every method this instance serves. Upgrade the instance if the client needs a newer method.",
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
MOSTRA_HOST_INVALID_PARAMS: {
|
|
66
|
-
severity: "error",
|
|
67
|
-
phase: "host",
|
|
68
|
-
title: "Invalid method params",
|
|
69
|
-
hint: "Fix the params named in the message so they match the method's schema.",
|
|
70
|
-
docs: {
|
|
71
|
-
explanation:
|
|
72
|
-
"The request's `params` don't match the method's published schema: a required field is missing, a value has the wrong type, or a value is outside its allowed range. The message names the first problem found.",
|
|
73
|
-
fix: "Compare the request with the method's schema in `@mostra/protocol` (or the published JSON Schema) and correct the field named in the message.",
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
MOSTRA_HOST_INTERNAL_ERROR: {
|
|
77
|
-
severity: "error",
|
|
78
|
-
phase: "host",
|
|
79
|
-
title: "Unexpected failure in the instance",
|
|
80
|
-
hint: "Retry the request with the same request ID; if it fails again, report it with the instance's log.",
|
|
81
|
-
docs: {
|
|
82
|
-
explanation:
|
|
83
|
-
"The instance failed while handling the request, for a reason that isn't one of its known conditions. Durable state is unaffected: a request is acknowledged only after its ledger transaction commits. The details are in the instance's log (`logs/supervisor.log` in the home), not in the response.",
|
|
84
|
-
fix: "Retry the request with the same request ID; retries are idempotent. If the failure repeats, report it with the relevant lines of `logs/supervisor.log`.",
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
48
|
MOSTRA_HOST_INITIALIZE_ORDER: {
|
|
88
49
|
severity: "error",
|
|
89
50
|
phase: "host",
|
|
@@ -106,28 +67,6 @@ export const hostDiagnostics: AreaCatalog<HostDiagnosticCode> = {
|
|
|
106
67
|
fix: "Register only the ports listed in `capabilities.ports` of the `mostra/initialize` result. If the host needs the port, upgrade the instance to a release that supports it.",
|
|
107
68
|
},
|
|
108
69
|
},
|
|
109
|
-
MOSTRA_HOST_CONNECTION_CLOSED: {
|
|
110
|
-
severity: "error",
|
|
111
|
-
phase: "host",
|
|
112
|
-
title: "Connection closed",
|
|
113
|
-
hint: "Reconnect, then retry the request with the same request ID.",
|
|
114
|
-
docs: {
|
|
115
|
-
explanation:
|
|
116
|
-
"The connection to the other side closed before a response arrived, for example because the instance exited or restarted. The request may or may not have taken effect.",
|
|
117
|
-
fix: "Reconnect and send `mostra/initialize` again, then retry the request with the same request ID. Mutating methods are idempotent by request ID, so a retry never applies twice.",
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
MOSTRA_HOST_TIMEOUT: {
|
|
121
|
-
severity: "error",
|
|
122
|
-
phase: "host",
|
|
123
|
-
title: "Request timed out",
|
|
124
|
-
hint: "Retry the request with the same request ID, or allow it more time.",
|
|
125
|
-
docs: {
|
|
126
|
-
explanation:
|
|
127
|
-
"No response arrived within the time the caller allowed. The other side may still be working on the request, and it may still take effect.",
|
|
128
|
-
fix: "Retry with the same request ID, which returns the recorded result if the first attempt completed. Allow a longer timeout for slow operations.",
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
70
|
MOSTRA_HOST_SHUTDOWN_REFUSED: {
|
|
132
71
|
severity: "error",
|
|
133
72
|
phase: "host",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Diagnostic codes of the PLATFORM area: the operating systems and architectures Mostra runs on
|
|
3
|
-
* (D35, RT-07).
|
|
3
|
+
* (D35, D146, RT-07).
|
|
4
4
|
*/
|
|
5
5
|
import type { AreaCatalog } from "../spec.ts";
|
|
6
6
|
|
|
@@ -19,11 +19,11 @@ export const platformDiagnostics: AreaCatalog<PlatformDiagnosticCode> = {
|
|
|
19
19
|
severity: "error",
|
|
20
20
|
phase: "platform",
|
|
21
21
|
title: "Unsupported platform",
|
|
22
|
-
hint: "Run Mostra on macOS or
|
|
22
|
+
hint: "Run Mostra on macOS, glibc-based Linux or Windows, on an x64 or arm64 machine.",
|
|
23
23
|
docs: {
|
|
24
24
|
explanation:
|
|
25
|
-
"Mostra runs on macOS and
|
|
26
|
-
fix: "Run Mostra on a
|
|
25
|
+
"Mostra runs on macOS, Linux with glibc and Windows, each on x64 and arm64. On any other operating system or architecture (32-bit Windows, musl-based Linux such as Alpine, BSDs) every entry point stops with this code before it creates or changes anything, because the process model, the control endpoint, storage and the runtime bundle are built and tested only on those platforms. The message names the platform that was detected.",
|
|
26
|
+
fix: "Run Mostra on macOS, a glibc-based Linux or Windows, with an x64 or arm64 processor. On Alpine Linux, use a glibc-based image instead.",
|
|
27
27
|
},
|
|
28
28
|
},
|
|
29
29
|
// gen:entries
|
package/src/index.ts
CHANGED
|
@@ -123,6 +123,7 @@ export {
|
|
|
123
123
|
BundlePlatformSchema,
|
|
124
124
|
BundleSetup,
|
|
125
125
|
bundleAssetName,
|
|
126
|
+
isWindowsBundle,
|
|
126
127
|
} from "./bundle.ts";
|
|
127
128
|
export { canonicalJson } from "./canonical.ts";
|
|
128
129
|
export {
|
|
@@ -485,6 +486,7 @@ export {
|
|
|
485
486
|
type WaitResult,
|
|
486
487
|
waitMethod,
|
|
487
488
|
} from "./methods/wait/wait.ts";
|
|
489
|
+
export { receivable } from "./receive.ts";
|
|
488
490
|
export {
|
|
489
491
|
HOST_PROTOCOL_REVISIONS,
|
|
490
492
|
negotiateRevision,
|
package/src/receive.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading what a newer peer sends (HO-07, D32, #45). Within a revision a peer may add values to an
|
|
3
|
+
* enum and members to a union: the wire schemas' evolution rule counts both as additive
|
|
4
|
+
* (`tools/schemas/compat.ts`). A receiver checking such a message against the schemas it was built
|
|
5
|
+
* with would refuse the whole message for one value it doesn't know, so it checks it against
|
|
6
|
+
* {@link receivable}'s copy instead, as the Rust crate's wire types read an `Unknown` variant.
|
|
7
|
+
*/
|
|
8
|
+
import * as z from "zod";
|
|
9
|
+
|
|
10
|
+
/** A Zod node's definition, as far as {@link receivable} reads it. */
|
|
11
|
+
interface Def {
|
|
12
|
+
readonly type: string;
|
|
13
|
+
readonly [key: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const opened = new WeakMap<z.ZodType, z.ZodType>();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `schema` as a receiver of a newer peer's messages checks it: every string enum and every union
|
|
20
|
+
* of string literals accepts any string (an enum's own checks still run, and an enum-keyed record
|
|
21
|
+
* still needs every key it names); a discriminated union accepts, besides its members, an object
|
|
22
|
+
* whose discriminator is a string none of them has, kept whole; strict objects drop unknown fields
|
|
23
|
+
* rather than refuse them. Everything else (fields, types, required members, limits) is checked as
|
|
24
|
+
* before, so a message a newer peer couldn't have sent still fails, and a message `schema` accepts
|
|
25
|
+
* reads exactly as `schema` reads it: a union tries its members as built before their opened
|
|
26
|
+
* copies, and a discriminated union's members keep their own tags.
|
|
27
|
+
*
|
|
28
|
+
* The result keeps `schema`'s TypeScript type, the shape this side was built for: a value the type
|
|
29
|
+
* doesn't name means a newer peer. Code that branches on such a field handles the other case, and
|
|
30
|
+
* a gate (a verdict, a check, a delivery) passes only on a value it knows means passing, never on
|
|
31
|
+
* the absence of the one it knows means failing.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const parsed = receivable(definition.result).safeParse(raw);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function receivable<T>(schema: z.ZodType<T>): z.ZodType<T> {
|
|
38
|
+
return open(schema) as z.ZodType<T>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function open(schema: z.ZodType): z.ZodType {
|
|
42
|
+
const known = opened.get(schema);
|
|
43
|
+
if (known) return known;
|
|
44
|
+
// A recursive schema (Zod's getter idiom) reaches itself while it is being opened: it finds this
|
|
45
|
+
// stand-in, which reads the finished copy when a message is checked (Opus review of MT-01, 7).
|
|
46
|
+
let result: z.ZodType | undefined;
|
|
47
|
+
opened.set(
|
|
48
|
+
schema,
|
|
49
|
+
z.lazy(() => result ?? schema),
|
|
50
|
+
);
|
|
51
|
+
result = rebuild(schema);
|
|
52
|
+
opened.set(schema, result);
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Any string, with the checks `schema` carries (an enum's `.refine`, say). */
|
|
57
|
+
function anyString(schema: z.ZodType): z.ZodType {
|
|
58
|
+
const checks = defOf(schema)["checks"] as unknown[] | undefined;
|
|
59
|
+
return checks?.length ? cloned(z.string(), { checks }) : z.string();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function defOf(schema: z.ZodType): Def {
|
|
63
|
+
return (schema as unknown as { _zod: { def: Def } })._zod.def;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A copy of `schema` with some of its definition replaced; its checks and settings stay. */
|
|
67
|
+
function cloned(schema: z.ZodType, changes: Record<string, unknown>): z.ZodType {
|
|
68
|
+
return (schema as unknown as { clone(def: Def): z.ZodType }).clone({
|
|
69
|
+
...defOf(schema),
|
|
70
|
+
...changes,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The string values of a literal or enum node, or undefined when it has others. */
|
|
75
|
+
function strings(schema: z.ZodType): string[] | undefined {
|
|
76
|
+
const def = defOf(schema);
|
|
77
|
+
const values =
|
|
78
|
+
def.type === "literal"
|
|
79
|
+
? (def["values"] as unknown[])
|
|
80
|
+
: def.type === "enum"
|
|
81
|
+
? Object.values(def["entries"] as Record<string, unknown>)
|
|
82
|
+
: undefined;
|
|
83
|
+
return values?.every((value) => typeof value === "string") ? (values as string[]) : undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function rebuild(schema: z.ZodType): z.ZodType {
|
|
87
|
+
const def = defOf(schema);
|
|
88
|
+
switch (def.type) {
|
|
89
|
+
case "enum":
|
|
90
|
+
return strings(schema) ? anyString(schema) : schema;
|
|
91
|
+
case "optional":
|
|
92
|
+
case "nullable":
|
|
93
|
+
case "readonly":
|
|
94
|
+
case "default":
|
|
95
|
+
case "prefault":
|
|
96
|
+
case "nonoptional":
|
|
97
|
+
case "catch":
|
|
98
|
+
return cloned(schema, { innerType: open(def["innerType"] as z.ZodType) });
|
|
99
|
+
case "array":
|
|
100
|
+
return cloned(schema, { element: open(def["element"] as z.ZodType) });
|
|
101
|
+
case "object": {
|
|
102
|
+
const shape = def["shape"] as Record<string, z.ZodType>;
|
|
103
|
+
const catchall = def["catchall"] as z.ZodType | undefined;
|
|
104
|
+
return cloned(schema, {
|
|
105
|
+
shape: Object.fromEntries(Object.entries(shape).map(([key, value]) => [key, open(value)])),
|
|
106
|
+
// A strict object (catchall `never`) would refuse a field a newer peer adds: it is dropped
|
|
107
|
+
// instead. A loose object keeps passing its fields through, and a typed catchall keeps
|
|
108
|
+
// checking them (GPT review of MT-01: the bridge's host context passes fields through).
|
|
109
|
+
catchall:
|
|
110
|
+
catchall === undefined || defOf(catchall).type === "never" ? undefined : open(catchall),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
case "record": {
|
|
114
|
+
const keyType = def["keyType"] as z.ZodType;
|
|
115
|
+
// Keys from an enum: every one it names is still required, and a key a newer peer adds
|
|
116
|
+
// passes through, as Zod's loose records do (Opus review of MT-01, 2).
|
|
117
|
+
if (strings(keyType) && !def["partial"]) {
|
|
118
|
+
return cloned(schema, { valueType: open(def["valueType"] as z.ZodType), mode: "loose" });
|
|
119
|
+
}
|
|
120
|
+
return cloned(schema, {
|
|
121
|
+
keyType: open(keyType),
|
|
122
|
+
valueType: open(def["valueType"] as z.ZodType),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
case "tuple":
|
|
126
|
+
return cloned(schema, {
|
|
127
|
+
items: (def["items"] as z.ZodType[]).map(open),
|
|
128
|
+
rest: def["rest"] ? open(def["rest"] as z.ZodType) : def["rest"],
|
|
129
|
+
});
|
|
130
|
+
case "intersection":
|
|
131
|
+
return cloned(schema, {
|
|
132
|
+
left: open(def["left"] as z.ZodType),
|
|
133
|
+
right: open(def["right"] as z.ZodType),
|
|
134
|
+
});
|
|
135
|
+
case "pipe":
|
|
136
|
+
return cloned(schema, { in: open(def["in"] as z.ZodType) });
|
|
137
|
+
case "lazy": {
|
|
138
|
+
const getter = def["getter"] as () => z.ZodType;
|
|
139
|
+
return z.lazy(() => open(getter()));
|
|
140
|
+
}
|
|
141
|
+
case "union":
|
|
142
|
+
return union(schema, def);
|
|
143
|
+
default:
|
|
144
|
+
return schema;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function union(schema: z.ZodType, def: Def): z.ZodType {
|
|
149
|
+
const options = def["options"] as z.ZodType[];
|
|
150
|
+
// A union of string literals is an enum written another way.
|
|
151
|
+
if (options.every((option) => strings(option) !== undefined)) return anyString(schema);
|
|
152
|
+
// The members as built come first, so whatever they accept reads as before; their opened copies
|
|
153
|
+
// only take what none of them does (Opus review of MT-01, 1).
|
|
154
|
+
const anyOf = (members: z.ZodType[]): z.ZodType =>
|
|
155
|
+
z.union([...options, ...members] as [z.ZodType, ...z.ZodType[]]);
|
|
156
|
+
const discriminator = def["discriminator"] as string | undefined;
|
|
157
|
+
if (discriminator === undefined) return anyOf(options.map(open));
|
|
158
|
+
const tags = new Set<string>();
|
|
159
|
+
const members: z.ZodType[] = [];
|
|
160
|
+
for (const option of options) {
|
|
161
|
+
const shape = defOf(option)["shape"] as Record<string, z.ZodType> | undefined;
|
|
162
|
+
const tag = shape?.[discriminator];
|
|
163
|
+
const values = tag ? strings(tag) : undefined;
|
|
164
|
+
// A member whose tag isn't a string literal: no newer member can be told from a broken one.
|
|
165
|
+
if (!tag || !values) return anyOf(options.map(open));
|
|
166
|
+
for (const value of values) tags.add(value);
|
|
167
|
+
// An opened member keeps its own tag: widened, an enum tag would take every member's messages.
|
|
168
|
+
const member = open(option);
|
|
169
|
+
const memberShape = defOf(member)["shape"] as Record<string, z.ZodType>;
|
|
170
|
+
members.push(cloned(member, { shape: { ...memberShape, [discriminator]: tag } }));
|
|
171
|
+
}
|
|
172
|
+
const newer = z.looseObject({
|
|
173
|
+
[discriminator]: z.string().refine((value) => !tags.has(value)),
|
|
174
|
+
});
|
|
175
|
+
return anyOf([...members, newer]);
|
|
176
|
+
}
|
package/src/rpc/error.ts
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
* Errors on a JSON-RPC connection. Every error carries a diagnostic (INTERFACES §1, §6): the
|
|
3
3
|
* JSON-RPC `error.code` says which kind of failure it is, and `error.data` is the diagnostic.
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
type CoreHostDiagnosticCode,
|
|
7
|
+
coreHostDiagnostics,
|
|
8
|
+
} from "../diagnostics/codes/host-core.ts";
|
|
6
9
|
import { Diagnostic } from "../diagnostics/diagnostic.ts";
|
|
7
10
|
import { buildDiagnostic, type DiagnosticDetails } from "../diagnostics/spec.ts";
|
|
8
11
|
import type { JsonRpcErrorObject } from "./messages.ts";
|
|
@@ -95,13 +98,13 @@ export class RpcError extends Error {
|
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
/**
|
|
98
|
-
* Builds
|
|
99
|
-
*
|
|
101
|
+
* Builds one of the HOST-area diagnostics the JSON-RPC core raises, from their own catalog, so the
|
|
102
|
+
* core pulls neither the other areas' text nor the rest of HOST's into browser bundles (D83, #17).
|
|
100
103
|
*/
|
|
101
104
|
export function hostDiagnostic(
|
|
102
|
-
code:
|
|
105
|
+
code: CoreHostDiagnosticCode,
|
|
103
106
|
details: DiagnosticDetails,
|
|
104
107
|
docsBase?: string,
|
|
105
108
|
): Diagnostic {
|
|
106
|
-
return buildDiagnostic(code,
|
|
109
|
+
return buildDiagnostic(code, coreHostDiagnostics[code], details, docsBase);
|
|
107
110
|
}
|
package/src/rpc/peer.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import type { Diagnostic } from "../diagnostics/diagnostic.ts";
|
|
17
17
|
import type { MethodDefinition } from "../method.ts";
|
|
18
|
+
import { receivable } from "../receive.ts";
|
|
18
19
|
import { hostDiagnostic, JSON_RPC_ERROR_CODES, RpcError } from "./error.ts";
|
|
19
20
|
import {
|
|
20
21
|
classifyMessage,
|
|
@@ -185,7 +186,9 @@ export class RpcPeer {
|
|
|
185
186
|
|
|
186
187
|
/**
|
|
187
188
|
* Calls a protocol method: validates the params before sending and the result on arrival. A
|
|
188
|
-
* result that fails the method's schema rejects with `MOSTRA_HOST_MALFORMED`.
|
|
189
|
+
* result that fails the method's schema rejects with `MOSTRA_HOST_MALFORMED`. The result is
|
|
190
|
+
* read as {@link receivable} reads it, so an enum value or union member a newer peer added
|
|
191
|
+
* within the revision arrives as sent (#45).
|
|
189
192
|
*/
|
|
190
193
|
async call<P, R>(
|
|
191
194
|
definition: MethodDefinition<P, R>,
|
|
@@ -194,7 +197,7 @@ export class RpcPeer {
|
|
|
194
197
|
): Promise<R> {
|
|
195
198
|
const checked = parseParams(definition, params, this.#options.docsBase);
|
|
196
199
|
const raw = await this.request(definition.method, checked as JsonRpcParams, options);
|
|
197
|
-
const parsed = definition.result.safeParse(raw);
|
|
200
|
+
const parsed = receivable(definition.result).safeParse(raw);
|
|
198
201
|
if (parsed.success) return parsed.data;
|
|
199
202
|
throw new RpcError(
|
|
200
203
|
this.#diagnostic(
|