@isoloop/protocol 0.1.0-next.2 → 0.1.0-next.4

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +609 -11
  2. package/dist/index.js +278 -16
  3. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -44,7 +44,7 @@ export declare function envelopeGuard<E extends AnyEnvelope>(types: MessageTypes
44
44
  /** Exhaustiveness check for switches over message unions. */
45
45
  export declare function assertNever(x: never, message?: string): never;
46
46
  /** Every error code on the wire (design §3, §4.7, §6.1, §6.3; ADRs 0002, 0003, 0007, 0008). */
47
- export declare const ERROR_CODES: readonly ["protocol_mismatch", "timeout", "no_surface", "forbidden", "not_found", "unsupported", "cross_origin", "routing_disabled", "navigate_failed", "unknown_topic", "too_large", "invalid", "not_encodable", "quota_exceeded", "bus_unavailable", "not_requestable", "already_replied", "reply_invalid", "contracts_failed", "contracts_invalid", "contracts_mismatch", "reserved_topic", "seed_async", "seed_pending"];
47
+ export declare const ERROR_CODES: readonly ["protocol_mismatch", "timeout", "no_surface", "forbidden", "not_found", "unsupported", "cross_origin", "routing_disabled", "navigate_failed", "unknown_topic", "too_large", "invalid", "not_encodable", "quota_exceeded", "bus_unavailable", "not_requestable", "already_replied", "reply_invalid", "aborted", "no_responder", "contracts_failed", "contracts_invalid", "contracts_mismatch", "reserved_topic", "seed_async", "seed_pending"];
48
48
  export type ErrorCode = (typeof ERROR_CODES)[number];
49
49
  export declare function isErrorCode(x: unknown): x is ErrorCode;
50
50
  /** A schema issue, as reported with `invalid` (design §6.3). */
@@ -80,6 +80,15 @@ export type Grants = {
80
80
  subscribe: string[];
81
81
  /** Programmatic `navigate()` allowed (ADR 0002). Default false. */
82
82
  navigate?: boolean;
83
+ /**
84
+ * The inspection feed and its controls (ADR 0020, decision 6): the isolate
85
+ * gets `ctx.inspector` on a port of its own, which reads the feed and can
86
+ * restart, kill and block isolates. Default false. Never in a pool: spawning
87
+ * a pooled isolate with it fails with `invalid_config`, since a pool merges
88
+ * its members' grants.
89
+ * @experimental
90
+ */
91
+ inspect?: boolean;
83
92
  };
84
93
  /** Topics under this prefix belong to the kernel (ADR 0002). */
85
94
  export declare const SYSTEM_TOPIC_PREFIX = "isoloop.";
@@ -184,12 +193,18 @@ export type IdleReport = {
184
193
  */
185
194
  busyMs?: number;
186
195
  };
196
+ /** The longest `block` a runtime runs; a longer one is cut to this (ADR 0020, decision 4). */
197
+ export declare const MAX_BLOCK_MS = 1e4;
187
198
  /** Isolate port: host → isolate. */
188
199
  export type HostToIsolate = Envelope<"init", InitPayload> | Envelope<"event", SerializedEvent> | Envelope<"result", CallResult> | Envelope<"shutdown", {
189
200
  reason: ShutdownReason;
190
201
  graceMs: number;
191
202
  }> | Envelope<"reattach", {
192
203
  busPort: MessagePort;
204
+ }> | Envelope<"block", {
205
+ ms: number;
206
+ }> | Envelope<"inspect", {
207
+ port: MessagePort;
193
208
  }>;
194
209
  /** Isolate port: isolate → host. */
195
210
  export type IsolateToHost = Envelope<"ready", {
@@ -198,6 +213,14 @@ export type IsolateToHost = Envelope<"ready", {
198
213
  renderFmt: string;
199
214
  /** The Isoloop `major.minor` the isolate was built against, from `defineIsolate` (ADR 0012). */
200
215
  requires?: string;
216
+ /**
217
+ * True when the worker started from `@isoloop/runtime/worker`, the tiny
218
+ * entry no chunk imports. Only such a worker can start from a URL with
219
+ * a fragment of its own without loading the bootstrap twice, which is
220
+ * what memory per isolate needs (ADR 0020, decision 4). Runtimes
221
+ * before it leave it out.
222
+ */
223
+ entry?: true;
201
224
  }> | Envelope<"patch", {
202
225
  fmt: string;
203
226
  batch: unknown;
@@ -208,7 +231,6 @@ export type IsolateToHost = Envelope<"ready", {
208
231
  args: unknown[];
209
232
  }> | Envelope<"beat", {
210
233
  lagMs: number;
211
- heapBytes?: number;
212
234
  idle?: IdleReport;
213
235
  members?: Record<string, IdleReport>;
214
236
  }> | Envelope<"error", ErrorPayload> | Envelope<"bye", Record<string, never>>;
@@ -226,14 +248,38 @@ export type BusRecord = {
226
248
  /** Isolate id of the publisher. */
227
249
  producer: string;
228
250
  /**
229
- * Set on a `bus.request` record: the caller's isolate id and its request id.
230
- * A recipient answers with `reply { topic, offset }` (design §6.3).
251
+ * Set on a `bus.request` record: the caller's isolate id (`'host'` for
252
+ * `kernel.request`) and its request id. A recipient answers with
253
+ * `reply { topic, offset }` (design §6.3). `via` names the agent transport
254
+ * that made the call (ADR 0018, decision 4); an isolate's own `bus.request`
255
+ * never sets it.
231
256
  */
232
- req?: {
233
- from: string;
234
- id: string;
235
- };
257
+ req?: RecordReq;
236
258
  };
259
+ /**
260
+ * The agent transports that call through `kernel.request` (ADR 0018, decision 4):
261
+ * the dev MCP, WebMCP, and later the Builder.
262
+ *
263
+ * Self-declared by the caller: a label for logging and policy between
264
+ * cooperating code on one page. Never authorize anything by it.
265
+ * @experimental
266
+ */
267
+ export type RequestVia = "mcp" | "webmcp" | "builder";
268
+ /** Every {@link RequestVia}, for guards. @experimental */
269
+ export declare const REQUEST_VIA: readonly RequestVia[];
270
+ /** True for a known {@link RequestVia}. @experimental */
271
+ export declare function isRequestVia(x: unknown): x is RequestVia;
272
+ /** `BusRecord.req`: who asked, under which request id, and through which transport. */
273
+ export type RecordReq = {
274
+ from: string;
275
+ id: string;
276
+ via?: RequestVia;
277
+ };
278
+ /**
279
+ * True for a well-formed `BusRecord.req`: string `from` and `id`, and `via`
280
+ * absent or a known {@link RequestVia}.
281
+ */
282
+ export declare function isRecordReq(x: unknown): x is RecordReq;
237
283
  /**
238
284
  * Identifies a publish so a re-send after a bus crash isn't appended twice to
239
285
  * a persistent topic (ADR 0007, step 5): `epoch` names the publisher's
@@ -263,6 +309,7 @@ export type IsolateToBus = Envelope<"publish", {
263
309
  topic: string;
264
310
  offset: number;
265
311
  seq: number;
312
+ partial?: number;
266
313
  }[];
267
314
  }> | Envelope<"request", {
268
315
  topic: string;
@@ -299,10 +346,15 @@ export declare const MAX_COMMIT_INTERVAL_MS = 1e3;
299
346
  export declare const DEFAULT_CREDIT = 1e3;
300
347
  /** Why an interval is invalid, or `undefined` when it's an integer from 0 to 1,000. */
301
348
  export declare function commitIntervalProblem(ms: unknown): string | undefined;
302
- /** Where one subscription has settled to: the highest settled offset, and the delivery `seq` (ADR 0016, point 11). */
349
+ /**
350
+ * Where one subscription has settled to: the highest settled offset, the
351
+ * delivery `seq` settled whole (ADR 0016, point 11), and how many records of
352
+ * delivery `seq + 1` are settled, when some are (point 17).
353
+ */
303
354
  export type CommitPoint = {
304
355
  offset: number;
305
356
  seq: number;
357
+ partial?: number;
306
358
  };
307
359
  export type CommitOffsets = ({
308
360
  topic: string;
@@ -311,7 +363,9 @@ export type CommitOffsets = ({
311
363
  * The commit point each record of one `deliver` settles to, in order. A
312
364
  * record settles its delivery `seq` only as the delivery's last record, and
313
365
  * `seq − 1` before that: a delivery counts once all its records are settled,
314
- * and records settle in the order they arrived. Its offset is the highest
366
+ * and records settle in the order they arrived. Before the last, the `i`-th
367
+ * record also settles the first `i + 1` records of its delivery (`partial`,
368
+ * ADR 0016, point 17). Its offset is the highest
315
369
  * delivered on the subscription up to it, `highest` being the highest before
316
370
  * this delivery (`-1` for none), so a commit that counts a delivery settled
317
371
  * also reaches every offset in it.
@@ -408,10 +462,16 @@ export type SeedEvent = {
408
462
  };
409
463
  /** Sampled every second on the control port (design §6.3). */
410
464
  export type BusMetrics = {
465
+ /**
466
+ * `head` is the next offset to be written; `retained` is how many records
467
+ * the topic still holds (ADR 0020, decision 2).
468
+ */
411
469
  topics: {
412
470
  topic: string;
413
471
  recordsPerSec: number;
414
472
  bytesPerSec: number;
473
+ head: number;
474
+ retained: number;
415
475
  }[];
416
476
  /** `lag` is `head − cursor`. */
417
477
  subscriptions: {
@@ -427,6 +487,41 @@ export type BusMetrics = {
427
487
  quota: number;
428
488
  };
429
489
  };
490
+ /** Longest preview of a record's value, in characters (ADR 0020, decision 3). */
491
+ export declare const PREVIEW_CHARS = 2048;
492
+ /** Records a `tap` sends each second when its `maxPerSec` isn't given. */
493
+ export declare const DEFAULT_TAP_PER_SEC = 50;
494
+ /** Most records one `read` returns. */
495
+ export declare const MAX_READ = 100;
496
+ /**
497
+ * A record as the inspection feed shows it (ADR 0020, decision 3): its value
498
+ * as text, cut to `PREVIEW_CHARS`, with the value's estimated size in bytes.
499
+ * @experimental
500
+ */
501
+ export type RecordPreview = {
502
+ topic: string;
503
+ offset: number;
504
+ key?: string;
505
+ ts: number;
506
+ producer: string;
507
+ req?: RecordReq;
508
+ /** The value's estimated size, as the bus counts it for `too_large` and the byte rates. */
509
+ bytes: number;
510
+ /** The value as JSON-like text, at most `PREVIEW_CHARS` characters. */
511
+ preview: string;
512
+ /** `preview` was cut short. */
513
+ truncated: boolean;
514
+ };
515
+ /**
516
+ * A tap's port: bus → host. `skipped` counts the records over the tap's
517
+ * `maxPerSec` since the last `skipped`, sent at most once a second.
518
+ * @experimental
519
+ */
520
+ export type BusToTap = Envelope<"record", RecordPreview> | Envelope<"skipped", {
521
+ count: number;
522
+ }>;
523
+ /** A tap's port: host → bus. `close` ends the tap. @experimental */
524
+ export type TapToBus = Envelope<"close", Record<string, never>>;
430
525
  /** Control port: host → bus worker. */
431
526
  export type HostToBus = Envelope<"boot", {
432
527
  contractsUrl: string;
@@ -453,9 +548,22 @@ export type HostToBus = Envelope<"boot", {
453
548
  topic: string;
454
549
  key?: string;
455
550
  value: unknown;
551
+ } & PublishId> | Envelope<"request", {
552
+ topic: string;
553
+ key?: string;
554
+ value: unknown;
555
+ timeoutMs: number;
556
+ via?: RequestVia;
456
557
  } & PublishId> | Envelope<"flush", Record<string, never>> | Envelope<"drop", {
457
558
  topic: string;
458
559
  force: boolean;
560
+ }> | Envelope<"tap", {
561
+ topics: string[];
562
+ maxPerSec?: number;
563
+ port: MessagePort;
564
+ }> | Envelope<"read", {
565
+ topic: string;
566
+ last: number;
459
567
  }>;
460
568
  /** Control port: bus worker → host. */
461
569
  export type BusToHost = Envelope<"ready", {
@@ -488,9 +596,17 @@ export type BusToHost = Envelope<"ready", {
488
596
  topic: string;
489
597
  }> | Envelope<"metrics", BusMetrics> | Envelope<"result", CallResult> | Envelope<"seed", SeedEvent> | Envelope<"error", ErrorPayload>;
490
598
  export declare const HOST_TO_BUS: MessageTypes<HostToBus>;
599
+ /** The message types a tap's port carries from the bus (ADR 0020, decision 3). @experimental */
600
+ export declare const BUS_TO_TAP: MessageTypes<BusToTap>;
601
+ /** The message types a tap's port carries to the bus. @experimental */
602
+ export declare const TAP_TO_BUS: MessageTypes<TapToBus>;
491
603
  export declare const BUS_TO_HOST: MessageTypes<BusToHost>;
492
604
  export declare const isHostToBus: (x: unknown) => x is HostToBus;
493
605
  export declare const isBusToHost: (x: unknown) => x is BusToHost;
606
+ /** A well-formed message from the bus on a tap's port. @experimental */
607
+ export declare const isBusToTap: (x: unknown) => x is BusToTap;
608
+ /** A well-formed message from the host on a tap's port. @experimental */
609
+ export declare const isTapToBus: (x: unknown) => x is TapToBus;
494
610
  /**
495
611
  * A Standard Schema V1 built by hand (ADR 0003, decision 6). Protocol depends
496
612
  * on nothing, so this is the structural subset of the interface that system
@@ -579,9 +695,33 @@ export type SchemaTypes<In, Out> = {
579
695
  export type TypedRecord<T> = Omit<BusRecord, "value"> & {
580
696
  value: T;
581
697
  };
698
+ /**
699
+ * Describes a requestable topic as a tool for agents (ADR 0018, decision 1).
700
+ * A `tool` field alone exposes nothing: the app's kernel config lists what
701
+ * agents may call (`agents.expose`). It stays on the host and isn't part of
702
+ * the `TopicConfig` the bus sees.
703
+ * @experimental
704
+ */
705
+ export type ToolSpec = {
706
+ /** A short human-readable name. The tool's name is always the topic's name. */
707
+ readonly title?: string;
708
+ /** What the tool does, for the agent choosing it. */
709
+ readonly description: string;
710
+ /** Required: `true` if a call changes nothing. Maps to `readOnlyHint` in WebMCP and MCP. */
711
+ readonly readOnly: boolean;
712
+ /** The call has effects the user should confirm. WebMCP `consequentialHint`, MCP `destructiveHint`. Default `false`. */
713
+ readonly consequential?: boolean;
714
+ /** The reply can carry third-party text. WebMCP `untrustedContentHint`. Default `false`. */
715
+ readonly untrustedContent?: boolean;
716
+ };
582
717
  /** The parts of a topic definition that make up its `TopicConfig`. */
583
718
  export type TopicShape = {
584
719
  readonly name: string;
720
+ /**
721
+ * The topic's Standard Schema. The host reads it only to make a tool's
722
+ * JSON Schema (ADR 0018, decision 1); it never validates with it.
723
+ */
724
+ readonly schema?: unknown;
585
725
  readonly retention: Retention;
586
726
  readonly maxRecordBytes?: number;
587
727
  readonly durability?: "memory" | "persistent";
@@ -592,6 +732,8 @@ export type TopicShape = {
592
732
  };
593
733
  readonly follow?: boolean;
594
734
  };
735
+ /** Host only: never part of the `TopicConfig` (ADR 0018, decision 1). */
736
+ readonly tool?: ToolSpec;
595
737
  };
596
738
  /** What `KernelConfig.contracts.list` holds: a `contracts()` list, seen structurally. */
597
739
  export type ContractsList = {
@@ -605,4 +747,460 @@ export type ContractsList = {
605
747
  export declare function topicConfigOf(t: TopicShape): TopicConfig;
606
748
  /** `"0.4.2"` → `"0.4"`. Anything that isn't `major.minor[.patch…]` gives `undefined`. */
607
749
  export declare function majorMinor(version: string): string | undefined;
608
- export declare function compareVersions(a: string, b: string): number;
750
+ export declare function compareVersions(a: string, b: string): number;
751
+ /**
752
+ * The vendor `unchecked()` in `@isoloop/bus` gives its schemas.
753
+ *
754
+ * @internal
755
+ */
756
+ export declare const UNCHECKED_VENDOR = "isoloop/unchecked";
757
+ /**
758
+ * The Standard JSON Schema V1 interface (https://standardschema.dev/json-schema),
759
+ * vendored like the bus's `StandardSchemaV1` so Isoloop depends on no schema
760
+ * library (ADR 0003, decision 21; ADR 0018, decision 1). Zod ≥ 4.2 implements
761
+ * it. Copied from the spec; keep it in step with it. Only the parts Isoloop
762
+ * reads are typed: the `jsonSchema` converter beside `validate`, without the
763
+ * spec's `Input`/`Output` type parameters, which Isoloop doesn't read.
764
+ *
765
+ * @internal
766
+ */
767
+ export interface StandardJSONSchemaV1 {
768
+ readonly "~standard": StandardJSONSchemaV1.Props;
769
+ }
770
+ /** @internal */
771
+ export declare namespace StandardJSONSchemaV1 {
772
+ export interface Props<Input = unknown, Output = Input> {
773
+ readonly version: 1;
774
+ readonly vendor: string;
775
+ readonly types?: {
776
+ readonly input: Input;
777
+ readonly output: Output;
778
+ } | undefined;
779
+ /** Methods that make the input and output JSON Schema. */
780
+ readonly jsonSchema: Converter;
781
+ }
782
+ export interface Converter {
783
+ /** The input type as JSON Schema. May throw if the target isn't supported. */
784
+ readonly input: (options: Options) => Record<string, unknown>;
785
+ /** The output type as JSON Schema. May throw if the target isn't supported. */
786
+ readonly output: (options: Options) => Record<string, unknown>;
787
+ }
788
+ export type Target = "draft-2020-12" | "draft-07" | "openapi-3.0" | ({} & string);
789
+ export interface Options {
790
+ readonly target: Target;
791
+ readonly libraryOptions?: Record<string, unknown> | undefined;
792
+ }
793
+ }
794
+ /**
795
+ * A JSON Schema document (draft 2020-12), as the schema library made it.
796
+ *
797
+ * @internal
798
+ */
799
+ export type JsonSchema = {
800
+ [key: string]: unknown;
801
+ };
802
+ /**
803
+ * A schema's JSON Schema, for what it accepts (`input`) and what it gives
804
+ * (`output`).
805
+ *
806
+ * @internal
807
+ */
808
+ export type JsonSchemaPair = {
809
+ readonly input: JsonSchema;
810
+ readonly output: JsonSchema;
811
+ };
812
+ /**
813
+ * Why a schema has no JSON Schema:
814
+ * - `unchecked`: it came from `unchecked()`, so there's nothing to describe;
815
+ * - `unsupported`: it doesn't implement `~standard.jsonSchema`;
816
+ * - `failed`: the converter threw (for example, a transform or a type JSON
817
+ * Schema can't express).
818
+ *
819
+ * @internal
820
+ */
821
+ export type NotExportableReason = "unchecked" | "unsupported" | "failed";
822
+ /**
823
+ * The result of {@link jsonSchemaOf}. Never throws; a failure is a value.
824
+ *
825
+ * @internal
826
+ */
827
+ export type JsonSchemaResult = ({
828
+ readonly ok: true;
829
+ } & JsonSchemaPair) | {
830
+ readonly ok: false;
831
+ readonly reason: NotExportableReason;
832
+ readonly message: string;
833
+ };
834
+ /**
835
+ * The JSON Schema target Isoloop uses everywhere (ADR 0003, decision 21).
836
+ *
837
+ * @internal
838
+ */
839
+ export declare const JSON_SCHEMA_TARGET = "draft-2020-12";
840
+ /**
841
+ * True if the schema implements Standard JSON Schema (`~standard.jsonSchema`).
842
+ *
843
+ * @internal
844
+ */
845
+ export declare function hasJsonSchema(schema: unknown): schema is StandardJSONSchemaV1;
846
+ /**
847
+ * Makes a schema's input and output JSON Schema (draft 2020-12) through the
848
+ * Standard JSON Schema interface. Never throws: an `unchecked()` schema, one
849
+ * without the interface, or a converter that throws gives `{ ok: false }`
850
+ * with the reason, so callers decide (`not_exposable` for the kernel, a flag
851
+ * for `isoloop contracts export`).
852
+ *
853
+ * @internal Used by the kernel's agent tools and `isoloop contracts export`.
854
+ */
855
+ export declare function jsonSchemaOf(schema: unknown): JsonSchemaResult;
856
+ /**
857
+ * A topic's JSON Schemas: its record schema, and its `reply` if it has one.
858
+ *
859
+ * @internal
860
+ */
861
+ export type TopicJsonSchemas = {
862
+ readonly schema: JsonSchemaPair;
863
+ readonly reply?: JsonSchemaPair;
864
+ };
865
+ /**
866
+ * The result of {@link topicJsonSchemas}. `part` says which schema failed.
867
+ *
868
+ * @internal
869
+ */
870
+ export type TopicJsonSchemasResult = ({
871
+ readonly ok: true;
872
+ } & TopicJsonSchemas) | {
873
+ readonly ok: false;
874
+ readonly topic: string;
875
+ readonly part: "schema" | "reply";
876
+ readonly reason: NotExportableReason;
877
+ /** Names the topic, e.g. "markets.quote.v1: reply is unchecked()". */
878
+ readonly message: string;
879
+ };
880
+ /**
881
+ * Makes a topic's JSON Schemas: the topic schema's input and output, and the
882
+ * `reply` schema's when it has one. A tool's input schema is `schema.input`;
883
+ * its output schema is `reply.output` (ADR 0018, decision 1). Never throws.
884
+ *
885
+ * @internal Used by the kernel's agent tools and `isoloop contracts export`.
886
+ */
887
+ export declare function topicJsonSchemas(def: {
888
+ readonly name: string;
889
+ readonly schema: unknown;
890
+ readonly reply?: unknown;
891
+ }): TopicJsonSchemasResult;
892
+ /**
893
+ * An exposed tool as the page sends it: `kernel.tools()` without `topic`.
894
+ * @internal
895
+ */
896
+ export type DevMcpTool = {
897
+ readonly name: string;
898
+ readonly title?: string;
899
+ readonly description: string;
900
+ readonly readOnly: boolean;
901
+ readonly consequential: boolean;
902
+ readonly untrustedContent: boolean;
903
+ readonly inputSchema: {
904
+ readonly [key: string]: unknown;
905
+ };
906
+ readonly outputSchema: {
907
+ readonly [key: string]: unknown;
908
+ };
909
+ readonly timeoutMs: number;
910
+ };
911
+ /** A call's answer: the reply, or the error's code and message. @internal */
912
+ export type DevMcpResult = {
913
+ readonly ok: true;
914
+ readonly value: unknown;
915
+ } | {
916
+ readonly ok: false;
917
+ /** A `BusError` code, or `unknown_tool` for a name the page doesn't expose. */
918
+ readonly code: string;
919
+ readonly message: string;
920
+ readonly issues?: readonly Issue[];
921
+ };
922
+ /** `hello`'s payload: the pairing token, the page's URL and title, and its tools. @internal */
923
+ export type DevMcpHello = {
924
+ readonly token: string;
925
+ readonly url: string;
926
+ readonly title?: string;
927
+ readonly tools: readonly DevMcpTool[];
928
+ /**
929
+ * The page answers `op` messages through `kernel.inspect()` (M4a task 8): the
930
+ * CLI then offers it the `isoloop_*` reads, and with `--allow-write` the
931
+ * writes. Absent: only the page's own tools.
932
+ */
933
+ readonly inspect?: boolean;
934
+ };
935
+ /** `welcome`'s payload: the page's id, which agents pass as `page`. @internal */
936
+ export type DevMcpWelcome = {
937
+ readonly page: string;
938
+ };
939
+ /** `call`'s payload: the tool to request and its input. @internal */
940
+ export type DevMcpCall = {
941
+ readonly name: string;
942
+ readonly input: unknown;
943
+ };
944
+ /**
945
+ * Page → CLI: `hello` first, then a `result` per call (`re` = the call's `id`).
946
+ * @internal
947
+ */
948
+ export type PageToCli = Envelope<"hello", DevMcpHello> | Envelope<"result", DevMcpResult>;
949
+ /**
950
+ * CLI → page: `welcome` once paired; `call` (an app tool) or `op` (one of the
951
+ * dev MCP's own tools), each with an `id`; `cancel`, `re` = a call's `id`,
952
+ * when the agent cancelled it (the page aborts its signal).
953
+ * @internal
954
+ */
955
+ export type CliToPage = Envelope<"welcome", DevMcpWelcome> | Envelope<"call", DevMcpCall> | Envelope<"op", DevMcpOp> | Envelope<"cancel", DevMcpCancel>;
956
+ /**
957
+ * `op`'s payload: one of the dev MCP's own tools, answered by the page from
958
+ * `kernel.inspect()` (ADR 0020) or the kernel (M4a decision 12). The page
959
+ * answers with a `result`, `re` = the op's `id`. The writes (`restart`,
960
+ * `kill`, `block`, `publish`, `request`) are sent only with `--allow-write`.
961
+ * @internal
962
+ */
963
+ export type DevMcpOp = {
964
+ readonly op: "isolates";
965
+ } | {
966
+ readonly op: "topics";
967
+ } | {
968
+ readonly op: "records";
969
+ readonly topic: string;
970
+ readonly last: number;
971
+ } | {
972
+ readonly op: "events";
973
+ readonly last: number;
974
+ } | {
975
+ readonly op: "restart" | "kill";
976
+ readonly id: string;
977
+ } | {
978
+ readonly op: "block";
979
+ readonly id: string;
980
+ readonly ms: number;
981
+ } | {
982
+ readonly op: "publish";
983
+ readonly topic: string;
984
+ readonly key?: string;
985
+ readonly value: unknown;
986
+ } | {
987
+ readonly op: "request";
988
+ readonly topic: string;
989
+ readonly key?: string;
990
+ readonly value: unknown;
991
+ readonly timeoutMs: number;
992
+ };
993
+ /** `DevMcpOp['op']`, as a list. @internal */
994
+ export declare const DEV_MCP_OPS: readonly DevMcpOp["op"][];
995
+ /** `cancel`'s payload: empty. @internal */
996
+ export type DevMcpCancel = Record<string, never>;
997
+ /** @internal */
998
+ export declare const PAGE_TO_CLI: MessageTypes<PageToCli>;
999
+ /** @internal */
1000
+ export declare const CLI_TO_PAGE: MessageTypes<CliToPage>;
1001
+ /** @internal */
1002
+ export declare const isPageToCli: (x: unknown) => x is PageToCli;
1003
+ /** @internal */
1004
+ export declare const isCliToPage: (x: unknown) => x is CliToPage;
1005
+ /**
1006
+ * WebSocket close codes the CLI uses (4000–4999 are the application's).
1007
+ * @internal
1008
+ */
1009
+ export declare const DEV_MCP_CLOSE: {
1010
+ /** The first message wasn't a well-formed `hello`, or its `v` differs. */
1011
+ readonly badHello: 4400;
1012
+ /** The token didn't match. */
1013
+ readonly badToken: 4401;
1014
+ /** The CLI is shutting down. */
1015
+ readonly shutdown: 4000;
1016
+ };
1017
+ /** The CLI's WebSocket port when `--port` isn't given. @internal */
1018
+ export declare const DEV_MCP_PORT = 4317;
1019
+ /**
1020
+ * One isolate in the feed, as `kernel.isolates()` has it.
1021
+ * `state` and `reason` are the kernel's `IsolateState` and `CrashReason`,
1022
+ * typed as strings here so a reader built against an older list still reads
1023
+ * a newer kernel's values.
1024
+ * @experimental
1025
+ */
1026
+ export type InspectIsolate = {
1027
+ id: string;
1028
+ state: string;
1029
+ pool?: string;
1030
+ /** `performance.now()` on the host when its last beat arrived. */
1031
+ lastBeatAt: number;
1032
+ lagMs: number;
1033
+ restarts: number;
1034
+ reason?: string;
1035
+ error?: string;
1036
+ /** Compositor figures for a visual isolate (`SurfaceStats`). */
1037
+ surface?: {
1038
+ queued: number;
1039
+ applied: number;
1040
+ rejected: number;
1041
+ dropped: number;
1042
+ lastFrameMs: number;
1043
+ };
1044
+ dropped: number;
1045
+ holds?: {
1046
+ ageMs: number;
1047
+ stack?: string;
1048
+ }[];
1049
+ busyMs?: number;
1050
+ /**
1051
+ * Its worker's memory, from the host's last measurement (ADR 0020, decision
1052
+ * 4). Absent when the page isn't cross-origin isolated, in engines without
1053
+ * `performance.measureUserAgentSpecificMemory()` (Firefox, Safari), and
1054
+ * before the first measurement.
1055
+ */
1056
+ memory?: InspectMemory;
1057
+ };
1058
+ /**
1059
+ * One isolate's memory (ADR 0020, decision 4): the bytes the browser
1060
+ * attributes to its worker. Coarse, and seconds old: Chromium answers a
1061
+ * measurement at its next garbage collection. A reader shows its age and
1062
+ * labels it "Chromium, cross-origin isolated".
1063
+ * @experimental
1064
+ */
1065
+ export type InspectMemory = {
1066
+ bytes: number;
1067
+ /** `performance.now()` on the host when the measurement came back. */
1068
+ at: number;
1069
+ /** True for a pool member: the figure is the whole pool's worker, shared by every member. */
1070
+ shared?: true;
1071
+ };
1072
+ /** One pool in the feed, as `kernel.pools()` has it. @experimental */
1073
+ export type InspectPool = {
1074
+ name: string;
1075
+ members: string[];
1076
+ running: boolean;
1077
+ grants: Grants;
1078
+ sharesDocument: boolean;
1079
+ warning?: string;
1080
+ };
1081
+ /**
1082
+ * One kernel event, at once or from the ring buffer (ADR 0020, decisions 2
1083
+ * and 5). `event` is a `KernelEvents` name (`isolate:crash`, `bus:restart`, …)
1084
+ * and `data` its payload. Beats and bus metrics come as their own messages.
1085
+ * @experimental
1086
+ */
1087
+ export type InspectEvent = {
1088
+ /** `performance.now()` on the host when it happened. */
1089
+ at: number;
1090
+ event: string;
1091
+ data: Record<string, unknown>;
1092
+ };
1093
+ /** One isolate's latest beat in a `beats` message. @experimental */
1094
+ export type InspectBeat = {
1095
+ id: string;
1096
+ pool?: string;
1097
+ lagMs: number;
1098
+ busyMs?: number;
1099
+ /** `performance.now()` on the host when it arrived. */
1100
+ at: number;
1101
+ };
1102
+ /**
1103
+ * The bus as the feed sees it. `state` is the bus supervisor's state
1104
+ * (`running`, `restarting`, `failed`, …), or `none` without contracts.
1105
+ * `restarts` counts bus restarts since the kernel started.
1106
+ * @experimental
1107
+ */
1108
+ export type InspectBus = {
1109
+ state: string;
1110
+ restarts: number;
1111
+ /** The last `bus:metrics`, once one has arrived. */
1112
+ metrics?: BusMetrics;
1113
+ };
1114
+ /** What a reader gets first (ADR 0020, decision 2). @experimental */
1115
+ export type InspectSnapshot = {
1116
+ /** `performance.now()` on the host when it was taken. */
1117
+ at: number;
1118
+ isolates: InspectIsolate[];
1119
+ pools: InspectPool[];
1120
+ bus: InspectBus;
1121
+ /** The ring buffer: the last 200 events since the kernel started, oldest first. */
1122
+ events: InspectEvent[];
1123
+ };
1124
+ /**
1125
+ * The feed's messages, in the usual envelope (ADR 0020, decision 7):
1126
+ * - `snapshot`, on request: an isolate's `ctx.inspector.snapshot()` gets it
1127
+ * over its `inspect` port, as the reply to its `snapshot` call;
1128
+ * - `event`, a kernel event as it happens;
1129
+ * - `beats`, each isolate's latest beat, coalesced to about one a second;
1130
+ * - `metrics`, the latest `bus:metrics`, at most one a second;
1131
+ * - `memory`, each isolate's memory after a measurement, at most one every
1132
+ * 5 s, and only where the page is cross-origin isolated (decision 4).
1133
+ * @experimental
1134
+ */
1135
+ export type InspectMessage = Envelope<"snapshot", InspectSnapshot> | Envelope<"event", InspectEvent> | Envelope<"beats", {
1136
+ at: number;
1137
+ beats: InspectBeat[];
1138
+ }> | Envelope<"metrics", BusMetrics> | Envelope<"memory", {
1139
+ at: number;
1140
+ isolates: {
1141
+ id: string;
1142
+ memory: InspectMemory;
1143
+ }[];
1144
+ }>;
1145
+ /** @experimental */
1146
+ export declare const INSPECT_MESSAGES: MessageTypes<InspectMessage>;
1147
+ /**
1148
+ * True for an envelope of a feed message this build knows. A reader drops
1149
+ * anything else, which is how it ignores message types added later.
1150
+ * @experimental
1151
+ */
1152
+ export declare const isInspectMessage: (x: unknown) => x is InspectMessage;
1153
+ /** How many events the kernel keeps for late readers (ADR 0020, decision 5). */
1154
+ export declare const INSPECT_RING_SIZE = 200;
1155
+ /** How often beats and bus metrics are coalesced (ADR 0020, decision 2). */
1156
+ export declare const INSPECT_COALESCE_MS = 1e3;
1157
+ /** The shortest time between two memory measurements (ADR 0020, decision 4). */
1158
+ export declare const INSPECT_MEMORY_MS = 5e3;
1159
+ /**
1160
+ * One record from a tap or a read (ADR 0020, decision 3): its value cut to a
1161
+ * 2 KB preview, with its full size.
1162
+ * @experimental
1163
+ */
1164
+ export type InspectRecord = RecordPreview;
1165
+ /** What an isolate asks over its `inspect` port (ADR 0020, decision 6). @experimental */
1166
+ export type InspectControl = "restart" | "kill" | "block";
1167
+ /**
1168
+ * The `inspect` port: isolate → host (ADR 0020, decision 6). Each message with
1169
+ * an `id` gets a `result` (`snapshot` gets a `snapshot`) with `re` set to it.
1170
+ * - `listen` / `unlisten`: start and stop the feed's `event`, `beats` and
1171
+ * `metrics` messages on the port;
1172
+ * - `snapshot`: one `InspectSnapshot`;
1173
+ * - `records`: the last `last` records of `topic` (at most 100), as
1174
+ * `InspectRecord`s in the result's `value`;
1175
+ * - `tap` / `untap`: copy matching records onto the port as `record`
1176
+ * messages, `untap`'s `re` naming the tap's `id`;
1177
+ * - `control`: restart, kill or block (`ms`, at most 10 s) an isolate.
1178
+ * @experimental
1179
+ */
1180
+ export type IsolateToInspect = Envelope<"listen", Record<string, never>> | Envelope<"unlisten", Record<string, never>> | Envelope<"snapshot", Record<string, never>> | Envelope<"records", {
1181
+ topic: string;
1182
+ last: number;
1183
+ }> | Envelope<"tap", {
1184
+ topics: string[];
1185
+ maxPerSec?: number;
1186
+ }> | Envelope<"untap", Record<string, never>> | Envelope<"control", {
1187
+ action: InspectControl;
1188
+ id: string;
1189
+ ms?: number;
1190
+ }>;
1191
+ /**
1192
+ * The `inspect` port: host → isolate. The feed's own messages, a call's
1193
+ * `result`, and a tap's `record` and `skipped` (with `re` naming the tap).
1194
+ * @experimental
1195
+ */
1196
+ export type InspectToIsolate = InspectMessage | Envelope<"result", CallResult> | Envelope<"record", InspectRecord> | Envelope<"skipped", {
1197
+ count: number;
1198
+ }>;
1199
+ /** @experimental */
1200
+ export declare const ISOLATE_TO_INSPECT: MessageTypes<IsolateToInspect>;
1201
+ /** @experimental */
1202
+ export declare const INSPECT_TO_ISOLATE: MessageTypes<InspectToIsolate>;
1203
+ /** @experimental */
1204
+ export declare const isIsolateToInspect: (x: unknown) => x is IsolateToInspect;
1205
+ /** @experimental */
1206
+ export declare const isInspectToIsolate: (x: unknown) => x is InspectToIsolate;
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ const PROTOCOL = {
4
4
  v: 1,
5
5
  min: 1
6
6
  };
7
- const isRecord = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
7
+ const isRecord$1 = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
8
8
  const isOptionalString = (x) => x === void 0 || typeof x === "string";
9
9
  /**
10
10
  * True for a well-formed envelope this build can read: `v` is a supported
@@ -13,7 +13,7 @@ const isOptionalString = (x) => x === void 0 || typeof x === "string";
13
13
  * Says nothing about whether `t` is known; see the per-port guards.
14
14
  */
15
15
  function isEnvelope(x) {
16
- return isRecord(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]) && isOptionalString(x["isolate"]);
16
+ return isRecord$1(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]) && isOptionalString(x["isolate"]);
17
17
  }
18
18
  /**
19
19
  * Builds a guard for one port's union from its message types. Each side drops
@@ -48,6 +48,8 @@ const ERROR_CODES = [
48
48
  "not_requestable",
49
49
  "already_replied",
50
50
  "reply_invalid",
51
+ "aborted",
52
+ "no_responder",
51
53
  "contracts_failed",
52
54
  "contracts_invalid",
53
55
  "contracts_mismatch",
@@ -86,12 +88,16 @@ function matchAny(patterns, topic) {
86
88
  return patterns.some((p) => matchTopic(p, topic));
87
89
  }
88
90
  // src/isolate.ts
91
+ /** The longest `block` a runtime runs; a longer one is cut to this (ADR 0020, decision 4). */
92
+ const MAX_BLOCK_MS = 1e4;
89
93
  const HOST_TO_ISOLATE = {
90
94
  init: true,
91
95
  event: true,
92
96
  result: true,
93
97
  shutdown: true,
94
- reattach: true
98
+ reattach: true,
99
+ block: true,
100
+ inspect: true
95
101
  };
96
102
  const ISOLATE_TO_HOST = {
97
103
  ready: true,
@@ -104,6 +110,25 @@ const ISOLATE_TO_HOST = {
104
110
  const isHostToIsolate = envelopeGuard(HOST_TO_ISOLATE);
105
111
  const isIsolateToHost = envelopeGuard(ISOLATE_TO_HOST);
106
112
  // src/bus.ts
113
+ /** Every {@link RequestVia}, for guards. @experimental */
114
+ const REQUEST_VIA = Object.freeze([
115
+ "mcp",
116
+ "webmcp",
117
+ "builder"
118
+ ]);
119
+ /** True for a known {@link RequestVia}. @experimental */
120
+ function isRequestVia(x) {
121
+ return typeof x === "string" && REQUEST_VIA.includes(x);
122
+ }
123
+ /**
124
+ * True for a well-formed `BusRecord.req`: string `from` and `id`, and `via`
125
+ * absent or a known {@link RequestVia}.
126
+ */
127
+ function isRecordReq(x) {
128
+ if (typeof x !== "object" || x === null) return false;
129
+ const via = Reflect.get(x, "via");
130
+ return typeof Reflect.get(x, "from") === "string" && typeof Reflect.get(x, "id") === "string" && (via === void 0 || isRequestVia(via));
131
+ }
107
132
  const ISOLATE_TO_BUS = {
108
133
  publish: true,
109
134
  subscribe: true,
@@ -135,7 +160,9 @@ function commitIntervalProblem(ms) {
135
160
  * The commit point each record of one `deliver` settles to, in order. A
136
161
  * record settles its delivery `seq` only as the delivery's last record, and
137
162
  * `seq − 1` before that: a delivery counts once all its records are settled,
138
- * and records settle in the order they arrived. Its offset is the highest
163
+ * and records settle in the order they arrived. Before the last, the `i`-th
164
+ * record also settles the first `i + 1` records of its delivery (`partial`,
165
+ * ADR 0016, point 17). Its offset is the highest
139
166
  * delivered on the subscription up to it, `highest` being the highest before
140
167
  * this delivery (`-1` for none), so a commit that counts a delivery settled
141
168
  * also reaches every offset in it.
@@ -144,12 +171,38 @@ function deliveryPoints(seq, offsets, highest) {
144
171
  let top = highest;
145
172
  return offsets.map((offset, i) => {
146
173
  top = Math.max(top, offset);
147
- return {
174
+ return i === offsets.length - 1 ? {
175
+ offset: top,
176
+ seq
177
+ } : {
148
178
  offset: top,
149
- seq: i === offsets.length - 1 ? seq : seq - 1
179
+ seq: seq - 1,
180
+ partial: i + 1
150
181
  };
151
182
  });
152
183
  }
184
+ const point = ({ offset, seq, partial }) => partial ? {
185
+ offset,
186
+ seq,
187
+ partial
188
+ } : {
189
+ offset,
190
+ seq
191
+ };
192
+ /**
193
+ * The further of two commit points: the highest offset, and the delivery
194
+ * reached furthest, a whole delivery being further than any part of it.
195
+ */
196
+ function later(a, b) {
197
+ const offset = Math.max(a.offset, b.offset);
198
+ const reach = (p) => [p.seq, p.partial ?? 0];
199
+ const [as, ap] = reach(a);
200
+ const [bs, bp] = reach(b);
201
+ return point({
202
+ ...bs > as || bs === as && bp >= ap ? b : a,
203
+ offset
204
+ });
205
+ }
153
206
  function createCommitter(o) {
154
207
  const intervalMs = o.intervalMs ?? 100;
155
208
  const half = Math.max(1, Math.floor((o.credit ?? 1e3) / 2));
@@ -167,7 +220,12 @@ function createCommitter(o) {
167
220
  queued = false;
168
221
  if (offsets.size === 0) return void 0;
169
222
  const out = [];
170
- for (const [topic, { offset, seq }] of offsets) out.push({
223
+ for (const [topic, { offset, seq, partial }] of offsets) out.push(partial ? {
224
+ topic,
225
+ offset,
226
+ seq,
227
+ partial
228
+ } : {
171
229
  topic,
172
230
  offset,
173
231
  seq
@@ -194,13 +252,7 @@ function createCommitter(o) {
194
252
  return {
195
253
  settle(topic, at, records = 1) {
196
254
  const prev = offsets.get(topic);
197
- offsets.set(topic, prev === void 0 ? {
198
- offset: at.offset,
199
- seq: at.seq
200
- } : {
201
- offset: Math.max(prev.offset, at.offset),
202
- seq: Math.max(prev.seq, at.seq)
203
- });
255
+ offsets.set(topic, prev === void 0 ? point(at) : later(prev, at));
204
256
  const n = (counts.get(topic) ?? 0) + records;
205
257
  counts.set(topic, n);
206
258
  if (n >= half) flush();
@@ -236,14 +288,30 @@ var BusError = class extends Error {
236
288
  }
237
289
  };
238
290
  // src/control.ts
291
+ /** Longest preview of a record's value, in characters (ADR 0020, decision 3). */
292
+ const PREVIEW_CHARS = 2048;
293
+ /** Records a `tap` sends each second when its `maxPerSec` isn't given. */
294
+ const DEFAULT_TAP_PER_SEC = 50;
295
+ /** Most records one `read` returns. */
296
+ const MAX_READ = 100;
239
297
  const HOST_TO_BUS = {
240
298
  boot: true,
241
299
  attach: true,
242
300
  detach: true,
243
301
  publish: true,
302
+ request: true,
244
303
  flush: true,
245
- drop: true
304
+ drop: true,
305
+ tap: true,
306
+ read: true
307
+ };
308
+ /** The message types a tap's port carries from the bus (ADR 0020, decision 3). @experimental */
309
+ const BUS_TO_TAP = {
310
+ record: true,
311
+ skipped: true
246
312
  };
313
+ /** The message types a tap's port carries to the bus. @experimental */
314
+ const TAP_TO_BUS = { close: true };
247
315
  const BUS_TO_HOST = {
248
316
  ready: true,
249
317
  beat: true,
@@ -255,6 +323,10 @@ const BUS_TO_HOST = {
255
323
  };
256
324
  const isHostToBus = envelopeGuard(HOST_TO_BUS);
257
325
  const isBusToHost = envelopeGuard(BUS_TO_HOST);
326
+ /** A well-formed message from the bus on a tap's port. @experimental */
327
+ const isBusToTap = envelopeGuard(BUS_TO_TAP);
328
+ /** A well-formed message from the host on a tap's port. @experimental */
329
+ const isTapToBus = envelopeGuard(TAP_TO_BUS);
258
330
  // src/system-topics.ts
259
331
  /** Wraps a hand-written check as a Standard Schema that passes valid values through unchanged. */
260
332
  function systemSchema(check) {
@@ -354,4 +426,194 @@ function compareVersions(a, b) {
354
426
  const [bm, bn] = parse(b);
355
427
  return am - bm || an - bn;
356
428
  }
357
- export { BUS_TO_HOST, BUS_TO_ISOLATE, BusError, DEFAULT_COMMIT_INTERVAL_MS, DEFAULT_CREDIT, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, MAX_COMMIT_INTERVAL_MS, PROTOCOL, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, assertNever, checks, commitIntervalProblem, compareVersions, createCommitter, deliveryPoints, envelopeGuard, isBusToHost, isBusToIsolate, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isIsolateToBus, isIsolateToHost, isSystemTopic, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf };
429
+ // src/json-schema.ts
430
+ /**
431
+ * The vendor `unchecked()` in `@isoloop/bus` gives its schemas.
432
+ *
433
+ * @internal
434
+ */
435
+ const UNCHECKED_VENDOR = "isoloop/unchecked";
436
+ /**
437
+ * The JSON Schema target Isoloop uses everywhere (ADR 0003, decision 21).
438
+ *
439
+ * @internal
440
+ */
441
+ const JSON_SCHEMA_TARGET = "draft-2020-12";
442
+ const isRecord = (x) => typeof x === "object" && x !== null;
443
+ /**
444
+ * True if the schema implements Standard JSON Schema (`~standard.jsonSchema`).
445
+ *
446
+ * @internal
447
+ */
448
+ function hasJsonSchema(schema) {
449
+ if (!isRecord(schema)) return false;
450
+ const std = schema["~standard"];
451
+ if (!isRecord(std)) return false;
452
+ const js = std["jsonSchema"];
453
+ return isRecord(js) && typeof js["input"] === "function" && typeof js["output"] === "function";
454
+ }
455
+ /** `~standard.vendor`, when the value has one. */
456
+ function vendorOf(schema) {
457
+ if (!isRecord(schema)) return void 0;
458
+ const std = schema["~standard"];
459
+ if (!isRecord(std)) return void 0;
460
+ const vendor = std["vendor"];
461
+ return typeof vendor === "string" ? vendor : void 0;
462
+ }
463
+ const messageOf = (e) => e instanceof Error ? e.message : String(e);
464
+ /**
465
+ * Makes a schema's input and output JSON Schema (draft 2020-12) through the
466
+ * Standard JSON Schema interface. Never throws: an `unchecked()` schema, one
467
+ * without the interface, or a converter that throws gives `{ ok: false }`
468
+ * with the reason, so callers decide (`not_exposable` for the kernel, a flag
469
+ * for `isoloop contracts export`).
470
+ *
471
+ * @internal Used by the kernel's agent tools and `isoloop contracts export`.
472
+ */
473
+ function jsonSchemaOf(schema) {
474
+ const vendor = vendorOf(schema);
475
+ if (vendor === "isoloop/unchecked") return {
476
+ ok: false,
477
+ reason: "unchecked",
478
+ message: "is unchecked()"
479
+ };
480
+ if (!hasJsonSchema(schema)) return {
481
+ ok: false,
482
+ reason: "unsupported",
483
+ message: `doesn't implement Standard JSON Schema (${vendor ?? "not a Standard Schema"})`
484
+ };
485
+ const options = { target: JSON_SCHEMA_TARGET };
486
+ try {
487
+ const { jsonSchema } = schema["~standard"];
488
+ return {
489
+ ok: true,
490
+ input: jsonSchema.input(options),
491
+ output: jsonSchema.output(options)
492
+ };
493
+ } catch (e) {
494
+ return {
495
+ ok: false,
496
+ reason: "failed",
497
+ message: `can't be made into JSON Schema: ${messageOf(e)}`
498
+ };
499
+ }
500
+ }
501
+ const pair = (r) => ({
502
+ input: r.input,
503
+ output: r.output
504
+ });
505
+ /**
506
+ * Makes a topic's JSON Schemas: the topic schema's input and output, and the
507
+ * `reply` schema's when it has one. A tool's input schema is `schema.input`;
508
+ * its output schema is `reply.output` (ADR 0018, decision 1). Never throws.
509
+ *
510
+ * @internal Used by the kernel's agent tools and `isoloop contracts export`.
511
+ */
512
+ function topicJsonSchemas(def) {
513
+ const fail = (part, r) => ({
514
+ ok: false,
515
+ topic: def.name,
516
+ part,
517
+ reason: r.reason,
518
+ message: `${def.name}: ${part} ${r.message}`
519
+ });
520
+ const schema = jsonSchemaOf(def.schema);
521
+ if (!schema.ok) return fail("schema", schema);
522
+ if (def.reply === void 0) return {
523
+ ok: true,
524
+ schema: pair(schema)
525
+ };
526
+ const reply = jsonSchemaOf(def.reply);
527
+ if (!reply.ok) return fail("reply", reply);
528
+ return {
529
+ ok: true,
530
+ schema: pair(schema),
531
+ reply: pair(reply)
532
+ };
533
+ }
534
+ // src/dev-mcp.ts
535
+ /** `DevMcpOp['op']`, as a list. @internal */
536
+ const DEV_MCP_OPS = [
537
+ "isolates",
538
+ "topics",
539
+ "records",
540
+ "events",
541
+ "restart",
542
+ "kill",
543
+ "block",
544
+ "publish",
545
+ "request"
546
+ ];
547
+ /** @internal */
548
+ const PAGE_TO_CLI = {
549
+ hello: true,
550
+ result: true
551
+ };
552
+ /** @internal */
553
+ const CLI_TO_PAGE = {
554
+ welcome: true,
555
+ call: true,
556
+ op: true,
557
+ cancel: true
558
+ };
559
+ /** @internal */
560
+ const isPageToCli = envelopeGuard(PAGE_TO_CLI);
561
+ /** @internal */
562
+ const isCliToPage = envelopeGuard(CLI_TO_PAGE);
563
+ /**
564
+ * WebSocket close codes the CLI uses (4000–4999 are the application's).
565
+ * @internal
566
+ */
567
+ const DEV_MCP_CLOSE = {
568
+ /** The first message wasn't a well-formed `hello`, or its `v` differs. */
569
+ badHello: 4400,
570
+ /** The token didn't match. */
571
+ badToken: 4401,
572
+ /** The CLI is shutting down. */
573
+ shutdown: 4e3
574
+ };
575
+ /** The CLI's WebSocket port when `--port` isn't given. @internal */
576
+ const DEV_MCP_PORT = 4317;
577
+ // src/inspect.ts
578
+ /** @experimental */
579
+ const INSPECT_MESSAGES = {
580
+ snapshot: true,
581
+ event: true,
582
+ beats: true,
583
+ metrics: true,
584
+ memory: true
585
+ };
586
+ /**
587
+ * True for an envelope of a feed message this build knows. A reader drops
588
+ * anything else, which is how it ignores message types added later.
589
+ * @experimental
590
+ */
591
+ const isInspectMessage = envelopeGuard(INSPECT_MESSAGES);
592
+ /** How many events the kernel keeps for late readers (ADR 0020, decision 5). */
593
+ const INSPECT_RING_SIZE = 200;
594
+ /** How often beats and bus metrics are coalesced (ADR 0020, decision 2). */
595
+ const INSPECT_COALESCE_MS = 1e3;
596
+ /** The shortest time between two memory measurements (ADR 0020, decision 4). */
597
+ const INSPECT_MEMORY_MS = 5e3;
598
+ /** @experimental */
599
+ const ISOLATE_TO_INSPECT = {
600
+ listen: true,
601
+ unlisten: true,
602
+ snapshot: true,
603
+ records: true,
604
+ tap: true,
605
+ untap: true,
606
+ control: true
607
+ };
608
+ /** @experimental */
609
+ const INSPECT_TO_ISOLATE = {
610
+ ...INSPECT_MESSAGES,
611
+ result: true,
612
+ record: true,
613
+ skipped: true
614
+ };
615
+ /** @experimental */
616
+ const isIsolateToInspect = envelopeGuard(ISOLATE_TO_INSPECT);
617
+ /** @experimental */
618
+ const isInspectToIsolate = envelopeGuard(INSPECT_TO_ISOLATE);
619
+ export { BUS_TO_HOST, BUS_TO_ISOLATE, BUS_TO_TAP, BusError, CLI_TO_PAGE, DEFAULT_COMMIT_INTERVAL_MS, DEFAULT_CREDIT, DEFAULT_TAP_PER_SEC, DEV_MCP_CLOSE, DEV_MCP_OPS, DEV_MCP_PORT, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, INSPECT_COALESCE_MS, INSPECT_MEMORY_MS, INSPECT_MESSAGES, INSPECT_RING_SIZE, INSPECT_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, ISOLATE_TO_INSPECT, JSON_SCHEMA_TARGET, MAX_BLOCK_MS, MAX_COMMIT_INTERVAL_MS, MAX_READ, PAGE_TO_CLI, PREVIEW_CHARS, PROTOCOL, REQUEST_VIA, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, TAP_TO_BUS, UNCHECKED_VENDOR, assertNever, checks, commitIntervalProblem, compareVersions, createCommitter, deliveryPoints, envelopeGuard, hasJsonSchema, isBusToHost, isBusToIsolate, isBusToTap, isCliToPage, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isInspectMessage, isInspectToIsolate, isIsolateToBus, isIsolateToHost, isIsolateToInspect, isPageToCli, isRecordReq, isRequestVia, isSystemTopic, isTapToBus, jsonSchemaOf, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf, topicJsonSchemas };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoloop/protocol",
3
- "version": "0.1.0-next.2",
3
+ "version": "0.1.0-next.4",
4
4
  "description": "Wire protocol types and guards shared by every Isoloop package.",
5
5
  "license": "MIT",
6
6
  "type": "module",