@isoplan/protocol 0.15.1 → 0.18.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/messages.js CHANGED
@@ -188,8 +188,8 @@ export const HelloMessageSchema = z.object({
188
188
  * runner can open them, and `orca` and `desktop` only where `orca` and `desktop` are said. The server
189
189
  * sends a start with a `mode` only to a runner that says `starts`, and only one of its places: a
190
190
  * runner from before 0.10.0 drops the field it doesn't know and would run the session from the floor.
191
- * `experts` (protocol 0.13.0): this runner hosts experts. It answers `hosting` with `experts`, and
192
- * takes `answer-expert` requests; the server sends neither to a runner that didn't say it.
191
+ * `experts` (protocol 0.13.0 to 0.16.0, the relay of experts' questions through the server) is gone:
192
+ * a 0.17.0 runner never says it, and an older runner's hello still parses, with it dropped.
193
193
  * `hiddenStarts` (protocol 0.14.0, isoplan-app#315): this runner can start a session from the floor
194
194
  * hidden (a start's `hidden`), in a clone Isoplan keeps for itself. The server sends `hidden` only to
195
195
  * a runner that says it: a runner from before 0.14.0 drops the field it doesn't know and would start
@@ -201,6 +201,13 @@ export const HelloMessageSchema = z.object({
201
201
  * `expert-creation-suggestion-dismissals`, `draft-office-wide-expert-creation-suggestion`,
202
202
  * `create-expert-from-suggestion` and `expert-creation-suggestion-ended`; the server sends none of
203
203
  * these to a runner that didn't say it.
204
+ * `expertInstances` (protocol 0.17.0, isoplan-app#340): this runner keeps instances of experts set
205
+ * up from their expert records and answers its own sessions' questions from them; it sends and
206
+ * takes the messages of "Expert records and instances" (see `expert-directory`). The server sends
207
+ * none of them to a runner that didn't say it.
208
+ * `sharesWhatExpertInstancesLearn` (protocol 0.18.0, isoplan-app#366): this runner sends what its
209
+ * instances' forks learn to their expert records, and takes what other instances learned, in the
210
+ * messages of "What instances learn". The server sends none of them to a runner that didn't say it.
204
211
  */
205
212
  export const RunnerHelloMessageSchema = z.object({
206
213
  type: z.literal("runner"),
@@ -213,9 +220,10 @@ export const RunnerHelloMessageSchema = z.object({
213
220
  drive: z.boolean().optional(),
214
221
  claude: ClaudeCliSchema.optional(),
215
222
  starts: z.array(StartModeSchema).max(4).optional(),
216
- experts: z.literal(true).optional(),
217
223
  hiddenStarts: z.literal(true).optional(),
218
224
  canCreateExpertsFromSuggestions: z.literal(true).optional(),
225
+ expertInstances: z.literal(true).optional(),
226
+ sharesWhatExpertInstancesLearn: z.literal(true).optional(),
219
227
  });
220
228
  /** All of the session's facts. Sent on connect and on repair. */
221
229
  export const FullMessageSchema = z.object({
@@ -243,11 +251,11 @@ export const MAX_OWNER_MESSAGE_LENGTH = 20000;
243
251
  * the plugin in the session never said whether it took the message (the session may have closed).
244
252
  */
245
253
  export const OwnerMessageRefusalSchema = z.enum(["not-owner", "detached", "no-submit", "ended", "dropped", "no-answer"]);
246
- // Experts (protocol 0.13.0): read-only agents a machine's runner hosts, each grounded in one repo,
247
- // which any session in an office that holds that repo may ask (ask_expert), from any machine,
248
- // through the office server. These shared pieces come before the reply, refusal and request schemas,
249
- // which use them.
250
- /** The most experts a runner lists, or a session is told it may ask (protocol 0.13.0). */
254
+ // Experts (protocol 0.13.0): read-only agents, each grounded in one repo, which any session in an
255
+ // office that holds that repo may ask (ask_expert). From 0.17.0 each computer keeps its own instance
256
+ // of an expert, set up from its expert record, and answers its own sessions' questions from it (see
257
+ // "Expert records and instances"). These shared pieces come before the messages that use them.
258
+ /** The most experts a session is told it may ask (protocol 0.13.0). */
251
259
  export const MAX_EXPERTS = 20;
252
260
  /** The longest subject an expert lists: what to ask it about, and before which changes. */
253
261
  export const MAX_EXPERT_SUBJECT_LENGTH = 1000;
@@ -261,22 +269,23 @@ export const ExpertNameSchema = z.string().regex(/^[a-z0-9][a-z0-9-]{0,62}$/, "l
261
269
  /** A full commit hash, as a handoff repo's. */
262
270
  const Commit = z.string().regex(/^([0-9a-f]{40}|[0-9a-f]{64})$/, "a full commit hash");
263
271
  const Subject = z.string().trim().min(1).max(MAX_EXPERT_SUBJECT_LENGTH);
264
- /** Every situation ask_expert tells its agent about, by a stable code. */
272
+ /**
273
+ * Every situation ask_expert tells its agent about, by a stable code. `EXPERT_NOT_SET_UP` (protocol
274
+ * 0.17.0): this computer has no instance of the expert yet, so its set-up is offered. No 0.17.0 side
275
+ * sends `EXPERT_QUEUED` or `EXPERT_DROPPED`; an office from before 0.17.0 may still send them in a
276
+ * late end. `EXPERT_QUESTION_HELD` is gone (0.17.0): a runner made it, and no office ever sent it.
277
+ */
265
278
  export const ExpertCodeSchema = z.enum([
266
279
  "EXPERT_RUNNER_UNREACHABLE",
267
280
  "EXPERT_UNKNOWN",
268
281
  "EXPERT_CAP",
269
- "EXPERT_QUESTION_HELD",
270
282
  "EXPERT_PLUGIN_OLD",
283
+ "EXPERT_NOT_SET_UP",
271
284
  "EXPERT_QUEUED",
272
285
  "EXPERT_STARTING",
273
286
  "EXPERT_FORK_FAILED",
274
287
  "EXPERT_DROPPED",
275
288
  ]);
276
- /** `starting`: no generation of it has passed its proof question yet; `ready`: its junior answers; `failed`: its last grounding failed and none answers. */
277
- export const ExpertStateSchema = z.enum(["starting", "ready", "failed"]);
278
- /** The host's caps, from the expert file: questions per asking session per day, open questions per asking session, the expert's daily token budget. */
279
- export const ExpertCapSchema = z.enum(["asker-day", "asker-open", "expert-tokens"]);
280
289
  /** The most folders or globs an expert's area names (protocol 0.15.0). */
281
290
  export const MAX_AREA_PATHS = 20;
282
291
  /**
@@ -285,32 +294,11 @@ export const MAX_AREA_PATHS = 20;
285
294
  */
286
295
  export const expertDisplayName = (key) => [...key.split("-").filter(Boolean).map((word) => word[0].toUpperCase() + word.slice(1)), "Expert"].join(" ");
287
296
  /**
288
- * One expert a runner hosts, as it lists it to an office server whose list takes its repo. `senior`
289
- * (protocol 0.14.0): it has a senior generation, which its junior can ask (see `expert-senior`);
290
- * absent from an older runner. `areas` (protocol 0.15.0): the folders and globs of its repo it
291
- * covers, from its file, so the office never suggests creating an expert on a folder one already covers;
292
- * absent from an older runner, or when its file names none. An older server ignores both.
297
+ * One expert a session may ask: `repo` is its project's short name, `host` a person's name: the
298
+ * hosting person's until 0.17.0, and from 0.17.0 the expert's owner's, kept so an older plugin still
299
+ * parses it.
293
300
  */
294
- export const ExpertListingSchema = z.object({
295
- name: ExpertNameSchema,
296
- project: ProjectSchema,
297
- subject: Subject,
298
- state: ExpertStateSchema,
299
- senior: z.boolean().optional(),
300
- areas: z.array(AreaPathSchema).max(MAX_AREA_PATHS).optional(),
301
- });
302
- /** One expert a session may ask: `repo` is its project's short name, `host` the hosting person's name. */
303
301
  export const ExpertRightSchema = z.object({ name: ExpertNameSchema, subject: Subject, repo: z.string().min(1), host: z.string() });
304
- /**
305
- * Why the host's runner won't answer an expert question, beside an `unavailable` refusal's `detail`:
306
- * one of the expert file's caps is reached (`frees`, when it frees), the fork couldn't answer, or this
307
- * machine hosts no expert by that name now.
308
- */
309
- export const ExpertRefusalSchema = z.discriminatedUnion("code", [
310
- z.object({ code: z.literal("EXPERT_CAP"), cap: ExpertCapSchema, limit: z.number().int().positive(), frees: z.iso.datetime({ offset: true }).optional() }),
311
- z.object({ code: z.literal("EXPERT_FORK_FAILED") }),
312
- z.object({ code: z.literal("EXPERT_UNKNOWN") }),
313
- ]);
314
302
  /** A cap on the answers a machine gives: a room's per hour, the owner's per day, or the owner's tokens per day. */
315
303
  export const CapSchema = z.enum(["room-hour", "owner-day", "owner-tokens"]);
316
304
  /** The answer to a request, carrying the request's id and kind. */
@@ -331,9 +319,6 @@ export const ReplyMessageSchema = z.discriminatedUnion("kind", [
331
319
  z.object({ type: z.literal("reply"), v: Version, id: RequestId, kind: z.literal("owner-message"), queued: z.literal(true).optional() }),
332
320
  // The handoff's session is in front in Orca now (protocol 0.3.8); one that couldn't be is refused `unavailable`.
333
321
  z.object({ type: z.literal("reply"), v: Version, id: RequestId, kind: z.literal("focus") }),
334
- // The expert's answer (protocol 0.13.0), its citation block and changed-since marks already in
335
- // `answer`; `commit` and `branch` are what the answer describes: the expert's read checkout then.
336
- z.object({ type: z.literal("reply"), v: Version, id: RequestId, kind: z.literal("answer-expert"), answer: z.string().min(1).max(MAX_EXPERT_ANSWER_LENGTH), commit: Commit, branch: BranchName }),
337
322
  ]);
338
323
  /**
339
324
  * A request the machine won't answer, sent in place of its reply. `over-cap` names the cap it would
@@ -364,9 +349,7 @@ export const RefusalMessageSchema = z.discriminatedUnion("reason", [
364
349
  unchecked: z.array(z.string().min(1)).optional(),
365
350
  }),
366
351
  // `code` (protocol 0.8.0 on): on an owner-message's refusal, why, for the floor to tell apart.
367
- // `expert` (protocol 0.13.0 on): on an answer-expert's refusal, why the host won't answer it, which
368
- // ends the question; without it, the server asks again later.
369
- z.object({ type: z.literal("refusal"), v: Version, id: RequestId, reason: z.literal("unavailable"), detail: z.string().min(1), code: OwnerMessageRefusalSchema.optional(), expert: ExpertRefusalSchema.optional() }),
352
+ z.object({ type: z.literal("refusal"), v: Version, id: RequestId, reason: z.literal("unavailable"), detail: z.string().min(1), code: OwnerMessageRefusalSchema.optional() }),
370
353
  ]);
371
354
  /**
372
355
  * The sender's session handed its room over with the command: its agent wrote the brief and pushed.
@@ -443,41 +426,6 @@ export const AskedAckMessageSchema = z.object({
443
426
  v: Version,
444
427
  question: RequestId,
445
428
  });
446
- /**
447
- * The experts this runner hosts (protocol 0.13.0), as it lists them to this office server, each one
448
- * whose repo the server's approved list takes. Sent only by a runner that said `experts`, only while
449
- * its link is up (never through the reply outbox), and never on a connection before that
450
- * connection's first `hosting`. The runner works the listing out again on every `hosting`, on every
451
- * change to its experts and every few minutes, and sends it when it differs from the last one it
452
- * sent on the connection; the first on a connection is always sent, even an empty one. It sends none
453
- * while it has no approved list. Each one replaces the last. Names are unique.
454
- */
455
- export const ExpertsMessageSchema = z
456
- .object({
457
- type: z.literal("experts"),
458
- v: Version,
459
- experts: z.array(ExpertListingSchema).max(MAX_EXPERTS),
460
- })
461
- .superRefine((message, context) => {
462
- const seen = new Set();
463
- message.experts.forEach((expert, i) => {
464
- if (seen.has(expert.name))
465
- context.addIssue({ code: "custom", path: ["experts", i, "name"], message: `the name ${expert.name} is listed once only` });
466
- seen.add(expert.name);
467
- });
468
- });
469
- /**
470
- * The junior answering the `answer-expert` request `id` has asked its senior (protocol 0.14.0), so
471
- * the floor can show the senior joining the question's meeting. It carries no text: nothing of the
472
- * question or of what the junior asked. Sent once per request, when the junior first asks, only to a
473
- * server whose `hosting` says `expertSenior`, and only while the link is up (never through the reply
474
- * outbox): a server from before 0.14.0, which doesn't say it, is never sent one.
475
- */
476
- export const ExpertSeniorMessageSchema = z.object({
477
- type: z.literal("expert-senior"),
478
- v: Version,
479
- id: RequestId,
480
- });
481
429
  /**
482
430
  * A session asks an expert a question (protocol 0.13.0), on its own short link with the session's
483
431
  * hello, as ask-room is. `key` is the tool call's id: asked again under it, it is the same question.
@@ -486,6 +434,8 @@ export const ExpertSeniorMessageSchema = z.object({
486
434
  * pairing codes, as for ask-room). It is a message of its own, not an ask-room with an expert, so a
487
435
  * server from before experts refuses it rather than asking a room. The session then polls it with
488
436
  * ask-check and acks its end with asked-ack, as for a room question.
437
+ * From 0.17.0 a server answers it only with an error `EXPERT_PLUGIN_OLD`, and a 0.17.0 plugin never
438
+ * sends it: it asks its own computer's instance of the expert (see `expert-question`).
489
439
  */
490
440
  export const AskExpertMessageSchema = z.object({
491
441
  type: z.literal("ask-expert"),
@@ -546,10 +496,17 @@ const Area = z.array(AreaPathSchema).min(1).max(MAX_AREA_PATHS);
546
496
  * and `proof`, as the runner's local model wrote them and the secret check passed them. `why`, the
547
497
  * signals that passed their thresholds, each other one 0: `share`, the share of the session's context
548
498
  * spent reading and searching the folder (0 to 1), and `tokens`, those tokens, which are one signal;
549
- * `minutes`, the time spent there; `corrections`, the person's corrections there. `at`: when it was
550
- * suggested. Of its text, only these leave the machine, under ADR 0004's case for an expert creation
551
- * suggestion's draft.
499
+ * `minutes`, the time spent there; `corrections`, the person's corrections there. `repetition`
500
+ * (from 0.16.0, a session's suggestion only), what was done in its area again, which suggested it:
501
+ * its `folder`, the file read most, was read in `readings` turns over `sessions` sessions, the
502
+ * area's `files` read again with it; or its folder was searched `searches` times. An older runner
503
+ * sends none, and an older side ignores it. `at`: when it was suggested. Of its text, only these
504
+ * leave the machine, under ADR 0004's case for an expert creation suggestion's draft.
552
505
  */
506
+ export const ExpertCreationSuggestionRepetitionSchema = z.union([
507
+ z.object({ readings: z.number().int().min(2), sessions: z.number().int().positive(), files: z.number().int().positive() }),
508
+ z.object({ searches: z.number().int().positive() }),
509
+ ]);
553
510
  export const ExpertCreationSuggestionMessageSchema = z.object({
554
511
  type: z.literal("expert-creation-suggestion"),
555
512
  v: Version,
@@ -568,6 +525,7 @@ export const ExpertCreationSuggestionMessageSchema = z.object({
568
525
  minutes: z.number().nonnegative(),
569
526
  corrections: z.number().int().nonnegative(),
570
527
  }),
528
+ repetition: ExpertCreationSuggestionRepetitionSchema.optional(),
571
529
  at: z.iso.datetime({ offset: true }),
572
530
  });
573
531
  /**
@@ -600,6 +558,297 @@ export const ExpertCreationProgressMessageSchema = z.object({
600
558
  state: ExpertCreationProgressStateSchema,
601
559
  detail: Detail.optional(),
602
560
  });
561
+ // Expert records and instances (protocol 0.17.0, isoplan-app#340): an expert's definition is kept in
562
+ // its expert record on the office server, which its owner's runner publishes from the expert file
563
+ // (`expert-definition`). Every computer whose sessions ask the expert keeps its own instance of it,
564
+ // set up from the record, and answers those questions itself: neither a question nor its answer
565
+ // leaves the computer, and the server is told only a record of each with no words
566
+ // (`expert-question`). A runner that can says `expertInstances` in its hello. The server sends such
567
+ // a runner `expert-directory` once that hello is let in, even an empty one, and whenever it changes,
568
+ // and sends none of the family to a runner that didn't say it. The runner sends nothing of the
569
+ // family on a connection before that connection's first `expert-directory`, so a server from before
570
+ // 0.17.0, which sends none, is never sent any. A runner sends the family only while its link is up,
571
+ // and counts a message sent only once it went: one that didn't, and one it must send again on a new
572
+ // connection, goes after that connection's first `expert-directory`. The server takes a repeat as
573
+ // the same message, so sending one again is always safe.
574
+ // Ownership is told, never worked out on the runner: the server marks what is this runner's
575
+ // person's own with `yours`, and names a person to the runner as the account that runner said hello
576
+ // as (`by`), since with sign-in on the server's id for a person isn't the Claude account the runner
577
+ // knows itself by.
578
+ /** The most expert records an `expert-directory` or an `expert-instance-states` names. */
579
+ export const MAX_EXPERT_RECORDS = 1000;
580
+ /** The most times an `expert-questions-asked-on-your-other-computers` lists for the last day. */
581
+ export const MAX_EXPERT_QUESTION_TIMES_IN_THE_LAST_DAY = 1000;
582
+ /** A definition's revision in its expert record: 1 when the record is made, one more at each change. */
583
+ const Revision = z.number().int().positive();
584
+ /**
585
+ * Which expert record: made by the server when the record is made, so a record made again under the
586
+ * key of a deleted one is told apart from the old one.
587
+ */
588
+ export const ExpertRecordIdSchema = z.uuid();
589
+ /** The record and revision a computer last had of the definition it publishes. */
590
+ const BasedOn = z.object({ expertRecordId: ExpertRecordIdSchema, revision: Revision });
591
+ /**
592
+ * What every computer's instance of an expert shares, as its owner's expert file says it: its key;
593
+ * its project, one with a remote, never a folder on one computer (`local:`); its subject; the model
594
+ * it grounds with; the context, in tokens, at which its junior is promoted (`promoteAtTokens`); the
595
+ * folders and globs of its repo it covers (`areas`); and its proof question with the answer expected.
596
+ * `learnedElsewhereTokensPerQuestion` (protocol 0.18.0, isoplan-app#366): the most tokens of what
597
+ * other instances learned that one fork's question carries, and that a new generation's grounding
598
+ * carries; absent is DEFAULT_LEARNED_ELSEWHERE_TOKENS_PER_QUESTION. A runner sends it only when the
599
+ * expert file sets it, so a definition that never set it is the same as before. An owner's computer
600
+ * from before 0.18.0 publishes the definition without it, which the server takes as a change, so
601
+ * every computer of a person is updated.
602
+ * The rest of an expert file is that computer's own, and never leaves it.
603
+ */
604
+ export const ExpertDefinitionSchema = z.object({
605
+ key: ExpertNameSchema,
606
+ project: ProjectSchema.refine((project) => !project.id.toLowerCase().startsWith("local:"), "a project with a remote: an expert's definition never names a folder on one computer"),
607
+ subject: Subject,
608
+ model: Model,
609
+ promoteAtTokens: z.number().int().positive(),
610
+ areas: z.array(AreaPathSchema).max(MAX_AREA_PATHS),
611
+ proof: Proof,
612
+ learnedElsewhereTokensPerQuestion: z.number().int().positive().max(1_000_000).optional(),
613
+ });
614
+ /**
615
+ * Where this computer's instance of the expert `key` stands, set up from (or, on its owner's
616
+ * computer, published as) the record `expertRecordId`: `setting-up`, with its `step` (`cloning`,
617
+ * finding this computer's checkout of the project or cloning it; `grounding`; `proof`); `ready`, its
618
+ * junior answers; or `failed`, with its `failure`: `clone`, the project couldn't be cloned, with
619
+ * git's words in `detail`, secret-checked on this computer; `proof`, its proof question failed;
620
+ * `key-used-here`, this computer has its own expert with that key, whose publish was refused
621
+ * `key-taken`, so the record can't be set up here until that one is renamed; `other`, with why in
622
+ * `detail`. `senior`: it has a senior generation, which its junior can ask. `groundingTokens`: its
623
+ * junior's context after grounding. No definition field: the record has it.
624
+ */
625
+ export const ExpertInstanceStateSchema = z.object({
626
+ key: ExpertNameSchema,
627
+ expertRecordId: ExpertRecordIdSchema,
628
+ state: z.enum(["setting-up", "ready", "failed"]),
629
+ step: z.enum(["cloning", "grounding", "proof"]).optional(),
630
+ senior: z.literal(true).optional(),
631
+ groundingTokens: z.number().int().positive().optional(),
632
+ failure: z.object({ why: z.enum(["clone", "proof", "key-used-here", "other"]), detail: Detail.optional() }).optional(),
633
+ });
634
+ /**
635
+ * One expert record in an `expert-directory`: one whose project an office holds. `owner` is the
636
+ * expert's owner, `yours` marks a record this runner's person owns, `revision` is its definition's
637
+ * revision now, and `groundingTokens` a junior's context after grounding, as an instance last said.
638
+ */
639
+ export const ExpertDirectoryEntrySchema = z.object({
640
+ key: ExpertNameSchema,
641
+ expertRecordId: ExpertRecordIdSchema,
642
+ project: ProjectSchema,
643
+ owner: Account,
644
+ yours: z.literal(true).optional(),
645
+ revision: Revision,
646
+ groundingTokens: z.number().int().positive().optional(),
647
+ });
648
+ /** Adds an issue for each entry of the list `field` that names a key an earlier entry named. */
649
+ const checkEachKeyOnce = (field, entries, context) => {
650
+ const seen = new Set();
651
+ entries.forEach((entry, i) => {
652
+ if (seen.has(entry.key))
653
+ context.addIssue({ code: "custom", path: [field, i, "key"], message: `the key ${entry.key} is named once only` });
654
+ seen.add(entry.key);
655
+ });
656
+ };
657
+ /**
658
+ * The owner's runner publishes the expert's definition from the expert file (protocol 0.17.0), after
659
+ * checking it for secrets on this computer (a match sends nothing). `basedOn`: the record and
660
+ * revision this computer last had of it from this server; absent for a file never published there.
661
+ * Sent on each new connection while the file differs from that revision, and on each change. The
662
+ * server answers `expert-definition-result`; the same definition and `basedOn` again get the same
663
+ * result.
664
+ */
665
+ export const ExpertDefinitionMessageSchema = z.object({
666
+ type: z.literal("expert-definition"),
667
+ v: Version,
668
+ definition: ExpertDefinitionSchema,
669
+ basedOn: BasedOn.optional(),
670
+ });
671
+ /**
672
+ * Send this computer the record `expertRecordId` of the expert `key` (protocol 0.17.0), answered
673
+ * with `expert-record`. Sent only once this computer has a checkout or clone of the record's
674
+ * project, and again after each new connection's first `expert-directory` while it is still wanted.
675
+ */
676
+ export const ExpertRecordWantedMessageSchema = z.object({
677
+ type: z.literal("expert-record-wanted"),
678
+ v: Version,
679
+ key: ExpertNameSchema,
680
+ expertRecordId: ExpertRecordIdSchema,
681
+ });
682
+ /**
683
+ * Every instance on this computer of this server's expert records whose project this server takes
684
+ * (protocol 0.17.0), the whole list each time: the first on each connection always, even an empty
685
+ * one, then only when it differs. An instance set up from one office server is never named to
686
+ * another. Each key once.
687
+ */
688
+ export const ExpertInstanceStatesMessageSchema = z
689
+ .object({
690
+ type: z.literal("expert-instance-states"),
691
+ v: Version,
692
+ instances: z.array(ExpertInstanceStateSchema).max(MAX_EXPERT_RECORDS),
693
+ })
694
+ .superRefine((message, context) => checkEachKeyOnce("instances", message.instances, context));
695
+ /**
696
+ * A question this computer's session asked the expert `key` (protocol 0.17.0), as a record with no
697
+ * words: never the question's or the answer's text. `id` is the tool call's id, `session` the asking
698
+ * session as its hello names it (`claude-code:<id>`), `blocking` whether its agent waited, `state`
699
+ * where it stands (`asking`, `answered` or `dropped`, with its `code`), `senior` that its junior asked
700
+ * its senior, and `askedAt` and `endedAt` when it was asked and ended. Sent at each change of state,
701
+ * and again after each new connection's first `expert-directory` until its end has gone. The same
702
+ * `id` and `state` again is the same record.
703
+ */
704
+ export const ExpertQuestionMessageSchema = z.object({
705
+ type: z.literal("expert-question"),
706
+ v: Version,
707
+ id: z.string().min(1).max(100),
708
+ key: ExpertNameSchema,
709
+ expertRecordId: ExpertRecordIdSchema,
710
+ session: z.string().min(1),
711
+ blocking: z.boolean(),
712
+ state: z.enum(["asking", "answered", "dropped"]),
713
+ code: ExpertCodeSchema.optional(),
714
+ senior: z.literal(true).optional(),
715
+ askedAt: z.iso.datetime({ offset: true }),
716
+ endedAt: z.iso.datetime({ offset: true }).optional(),
717
+ });
718
+ /**
719
+ * An agent on this computer asked the expert `key`, of which this computer has no instance, so its
720
+ * person is offered to set it up (protocol 0.17.0, ADR 0013). Sent once per computer per record,
721
+ * ever, and again after each new connection's first `expert-directory` until it has gone. A repeat
722
+ * for the same record, person and computer changes nothing. No text of the question.
723
+ */
724
+ export const ExpertSetUpOfferedMessageSchema = z.object({
725
+ type: z.literal("expert-set-up-offered"),
726
+ v: Version,
727
+ key: ExpertNameSchema,
728
+ expertRecordId: ExpertRecordIdSchema,
729
+ });
730
+ /**
731
+ * This computer's instance of the expert `key`, whose record isn't its person's own, failed its proof
732
+ * question at `revision` until its tries ran out (protocol 0.17.0), for the expert's owner to see:
733
+ * `answer`, what its fork answered, secret-checked on this computer, or `answerHeld`, that it matched
734
+ * a secret pattern and so isn't sent. Exactly one of the two. Sent once per revision, and again after
735
+ * each new connection's first `expert-directory` until it has gone. A repeat for the same record,
736
+ * computer and revision changes nothing.
737
+ */
738
+ export const ExpertProofFailedMessageSchema = z
739
+ .object({
740
+ type: z.literal("expert-proof-failed"),
741
+ v: Version,
742
+ key: ExpertNameSchema,
743
+ expertRecordId: ExpertRecordIdSchema,
744
+ revision: Revision,
745
+ answer: z.string().min(1).max(MAX_EXPERT_ANSWER_LENGTH).optional(),
746
+ answerHeld: z.literal(true).optional(),
747
+ })
748
+ .superRefine((message, context) => {
749
+ if ((message.answer === undefined) === (message.answerHeld === undefined))
750
+ context.addIssue({ code: "custom", path: ["answer"], message: "the fork's answer, or answerHeld: one of the two" });
751
+ });
752
+ // What instances learn (protocol 0.18.0, isoplan-app#366): what a fork of one computer's instance of
753
+ // an expert learns while answering reaches every other instance of it, through the expert record.
754
+ // The runner sends a thing learned to the record (`expert-instance-learned`): its text, the files it
755
+ // rests on with the commit each was read at, when it was learned, and an id the runner made; never
756
+ // the words of the question or of its answer. It checks it on its computer first, against the secret
757
+ // patterns and its personal-data patterns, and a match is never sent. The server keeps one of each
758
+ // id per record, so the same id again gets the same answer (`expert-instance-learned-received`), and
759
+ // numbers each on arrival: 1, 2, 3, … per record. A removal on the expert's card is an entry of the
760
+ // same sequence, numbered in turn, naming the thing removed and when, never who removed it. Over the
761
+ // sequence each side keeps a chain: sha256, in lowercase hex, of the chain before, "\n" and the
762
+ // entry's `arrivalEntryId`; "" before the first entry. Each side works it out itself, with sha256,
763
+ // since the floor's bundle imports this package too.
764
+ // A runner asks for a record's entries after its own last arrival number, with its own chain there
765
+ // (`expert-learned-wanted-after`). The server answers `expert-learned-since`, from 0 and marked
766
+ // `startedAgain` when that number is beyond its last or its chain there differs (a server restored
767
+ // from an older copy), and then pushes each new entry of the record on that connection; or, when it
768
+ // can't take the ask, `expert-learned-wanted-after-refused`, so the runner is never left waiting.
769
+ // A runner that can says `sharesWhatExpertInstancesLearn` in its hello, and the server says it in
770
+ // the `expert-directory` it sends such a runner. The server sends none of the family to a runner
771
+ // whose hello didn't say it; a runner sends none on a connection whose first `expert-directory`
772
+ // didn't, and only after a listing (`expert-instance-states`) on that connection, so the server
773
+ // knows the instance first. As for "Expert records and instances", a runner sends the family only
774
+ // while its link is up and counts a message sent only once it went.
775
+ // An older side: a server from before 0.18.0 doesn't say the flag, so a 0.18.0 runner sends it
776
+ // nothing of the family, keeps what its forks learn waiting, and carries nothing from elsewhere into
777
+ // its forks, until that server is updated. A runner from before 0.18.0 doesn't say it, so it is sent
778
+ // none: its instances learn only into their own trunks, as in 0.17.0, and it drops the directory's
779
+ // flag and a definition's `learnedElsewhereTokensPerQuestion` as keys it doesn't know.
780
+ /** The longest thing learned a runner sends: a fork's <learned> block is cut to this. */
781
+ export const MAX_WHAT_WAS_LEARNED_LENGTH = 3000;
782
+ /** The most files one thing learned names. */
783
+ export const MAX_FILES_WHAT_WAS_LEARNED_RESTS_ON = 50;
784
+ /** The most entries one `expert-learned-since` carries: more go in the messages after it. */
785
+ export const MAX_WHAT_WAS_LEARNED_ENTRIES_PER_MESSAGE = 100;
786
+ /** How much of what other instances learned one fork's question carries, in tokens, when the definition says none (D17). */
787
+ export const DEFAULT_LEARNED_ELSEWHERE_TOKENS_PER_QUESTION = 20_000;
788
+ /** Made by the runner that wrote it: the server keeps one of each per expert record. */
789
+ export const WhatWasLearnedIdSchema = z.uuid();
790
+ /** Numbered by the server as each entry reaches an expert record: 1, 2, 3, … per record. */
791
+ const ArrivalNumber = z.number().int().positive();
792
+ /** An arrival number, or 0 for none yet. */
793
+ const ArrivalNumberOrNone = z.number().int().nonnegative();
794
+ /** The chain at an arrival number: sha256, in lowercase hex, of the chain before, "\n" and the entry's `arrivalEntryId`; "" before the first entry. */
795
+ const ArrivalChain = z.union([z.literal(""), z.string().regex(/^[0-9a-f]{64}$/, "a sha256 in lowercase hex")]);
796
+ /** A file a thing learned rests on, and the commit it was read at; absent when unknown. */
797
+ export const FileWhatWasLearnedRestsOnSchema = z.object({ path: AreaPathSchema, commit: Commit.optional() });
798
+ /** Why the server can't take a thing learned or an ask: `record-gone`, the office holds no record of that id under the key; `not-an-instance-here`, this computer lists no instance of it. */
799
+ const WhatWasLearnedRefusal = z.enum(["record-gone", "not-an-instance-here"]);
800
+ /** Only `kept` carries an arrival number. */
801
+ const NoArrivalNumber = z.never().optional();
802
+ const whatWasLearned = {
803
+ id: WhatWasLearnedIdSchema,
804
+ text: z.string().min(1).max(MAX_WHAT_WAS_LEARNED_LENGTH),
805
+ files: z.array(FileWhatWasLearnedRestsOnSchema).min(1).max(MAX_FILES_WHAT_WAS_LEARNED_RESTS_ON),
806
+ learnedAt: z.iso.datetime({ offset: true }),
807
+ };
808
+ /**
809
+ * One entry of an expert record's sequence, in arrival order: `learned`, a thing an instance learned;
810
+ * `learned-then-removed`, one removed on the card since (its text and files gone); `removed`, the
811
+ * removal itself, of the thing `id`, at `at`. Who removed it stays on the server.
812
+ */
813
+ export const WhatWasLearnedEntrySchema = z.discriminatedUnion("kind", [
814
+ z.object({ kind: z.literal("learned"), arrivalNumber: ArrivalNumber, ...whatWasLearned }),
815
+ z.object({ kind: z.literal("learned-then-removed"), arrivalNumber: ArrivalNumber, id: WhatWasLearnedIdSchema }),
816
+ z.object({ kind: z.literal("removed"), arrivalNumber: ArrivalNumber, id: WhatWasLearnedIdSchema, at: z.iso.datetime({ offset: true }) }),
817
+ ]);
818
+ /** The id an entry adds to the chain: a thing learned's own id, or `removal:<id>` for its removal. */
819
+ export const arrivalEntryId = (entry) => (entry.kind === "removed" ? `removal:${entry.id}` : entry.id);
820
+ /**
821
+ * A fork of this computer's instance of the expert `key`, tied to the record `expertRecordId`, learned
822
+ * this (protocol 0.18.0): its `text`, cut to MAX_WHAT_WAS_LEARNED_LENGTH; the `files` it rests on,
823
+ * each with the commit it was read at where the runner knows it; `learnedAt`; and `id`, which the
824
+ * runner made. Never the question's or the answer's words. Sent once it passed both checks on this
825
+ * computer, after each listing this connection sends and again while unanswered or refused
826
+ * `not-an-instance-here`, until `expert-instance-learned-received` for its id comes `kept`,
827
+ * `removed` or refused `record-gone`. The same id again is the same thing learned.
828
+ */
829
+ export const ExpertInstanceLearnedMessageSchema = z.object({
830
+ type: z.literal("expert-instance-learned"),
831
+ v: Version,
832
+ key: ExpertNameSchema,
833
+ expertRecordId: ExpertRecordIdSchema,
834
+ ...whatWasLearned,
835
+ });
836
+ /**
837
+ * Send this computer every entry of the record `expertRecordId` after `afterArrivalNumber`, and push
838
+ * it the new ones on this connection (protocol 0.18.0), answered `expert-learned-since` or
839
+ * `expert-learned-wanted-after-refused`. Always this computer's own last arrival number for the
840
+ * record and its `chain` there: 0 and "" when it has none. Sent after a listing on this connection
841
+ * for each record an instance here is tied to that hasn't been asked for on it, again after a
842
+ * refusal `not-an-instance-here`, and to start again; one unanswered at a time per record.
843
+ */
844
+ export const ExpertLearnedWantedAfterMessageSchema = z.object({
845
+ type: z.literal("expert-learned-wanted-after"),
846
+ v: Version,
847
+ key: ExpertNameSchema,
848
+ expertRecordId: ExpertRecordIdSchema,
849
+ afterArrivalNumber: ArrivalNumberOrNone,
850
+ chain: ArrivalChain,
851
+ });
603
852
  const driven = { type: z.literal("driven"), v: Version };
604
853
  /**
605
854
  * Why a runner won't do what a `drive` request asked, sent in place of its answer (protocol 0.5.0).
@@ -768,12 +1017,18 @@ export const DataLayerMessageSchema = z.discriminatedUnion("type", [
768
1017
  AskCheckMessageSchema,
769
1018
  AskedAckMessageSchema,
770
1019
  DrivenMessageSchema,
771
- ExpertsMessageSchema,
772
1020
  AskExpertMessageSchema,
773
- ExpertSeniorMessageSchema,
774
1021
  ExpertCreationSuggestionMessageSchema,
775
1022
  RunnerExpertCreationSuggestionEndedMessageSchema,
776
1023
  ExpertCreationProgressMessageSchema,
1024
+ ExpertDefinitionMessageSchema,
1025
+ ExpertRecordWantedMessageSchema,
1026
+ ExpertInstanceStatesMessageSchema,
1027
+ ExpertQuestionMessageSchema,
1028
+ ExpertSetUpOfferedMessageSchema,
1029
+ ExpertProofFailedMessageSchema,
1030
+ ExpertInstanceLearnedMessageSchema,
1031
+ ExpertLearnedWantedAfterMessageSchema,
777
1032
  ]);
778
1033
  // Office server → data layer.
779
1034
  /** Asks the data layer to send its facts in full. */
@@ -880,22 +1135,6 @@ export const RequestMessageSchema = z.discriminatedUnion("kind", [
880
1135
  kind: z.literal("focus"),
881
1136
  handoff: HandoffId,
882
1137
  }),
883
- // A session asks one of this runner's experts (protocol 0.13.0), sent only to a runner that said
884
- // `experts`. `questionId` is the server's id for the question: the same across every request sent
885
- // for it, so the host answers it once, and the id its health log names. `asker` is the asking
886
- // session's id, which the expert file's caps count by; `from` is the asking person.
887
- z.object({
888
- type: z.literal("request"),
889
- v: Version,
890
- id: RequestId,
891
- kind: z.literal("answer-expert"),
892
- expert: ExpertNameSchema,
893
- questionId: RequestId,
894
- question: z.string().trim().min(1).max(MAX_QUESTION_LENGTH),
895
- blocking: z.boolean(),
896
- from: Account,
897
- asker: z.string().min(1),
898
- }),
899
1138
  ]);
900
1139
  /**
901
1140
  * The request is over (its handoff was cancelled, dismissed or replaced): the machine drops what it
@@ -974,7 +1213,8 @@ export const HandedOverMessageSchema = z.object({
974
1213
  * An answered or dropped question is sent again down the session's own link until it is acked.
975
1214
  * A question asked of an expert (protocol 0.13.0) has `expert`, and `owner` is the host's name; its
976
1215
  * `code` says where it stands for ask_expert, and an answered one has the `commit` and `branch` its
977
- * answer describes.
1216
+ * answer describes. From 0.17.0 no office sends one for an expert's question, which the asker's own
1217
+ * runner answers, though an office from before 0.17.0 may still send a late end.
978
1218
  */
979
1219
  export const AskedMessageSchema = z.object({
980
1220
  type: z.literal("asked"),
@@ -1014,21 +1254,6 @@ export const ExpertRightsMessageSchema = z.object({
1014
1254
  v: Version,
1015
1255
  experts: z.array(ExpertRightSchema).max(MAX_EXPERTS),
1016
1256
  });
1017
- /**
1018
- * Which of this runner's experts the server shows in at least one office (protocol 0.13.0), sent only
1019
- * to a runner that said `experts`: once its hello is let in (as the server last kept them, or none),
1020
- * after each `experts` message, and whenever the offices change. The runner sends no `experts` on a
1021
- * connection before that connection's first `hosting`, so a server from before experts, which sends
1022
- * none, is never sent one.
1023
- * `expertSenior` (protocol 0.14.0): this server reads `expert-senior`, so the runner may send it. A
1024
- * server that reads it says it on every `hosting`; the runner goes by the connection's last one.
1025
- */
1026
- export const HostingMessageSchema = z.object({
1027
- type: z.literal("hosting"),
1028
- v: Version,
1029
- listed: z.array(ExpertNameSchema).max(MAX_EXPERTS),
1030
- expertSenior: z.literal(true).optional(),
1031
- });
1032
1257
  const drive = { type: z.literal("drive"), v: Version, id: DriveId, by: Account };
1033
1258
  /**
1034
1259
  * A person acting on a driven session from the floor (protocol 0.5.0), sent only to a runner that said
@@ -1235,6 +1460,185 @@ export const ServerExpertCreationSuggestionEndedMessageSchema = z.discriminatedU
1235
1460
  z.object({ ...expertCreationSuggestionEnded, why: z.literal("made"), key: ExpertNameSchema }),
1236
1461
  z.object({ ...expertCreationSuggestionEnded, why: z.enum(["dismissed", "refused", "expired"]) }),
1237
1462
  ]);
1463
+ /**
1464
+ * Every expert record whose project an office on this server holds (protocol 0.17.0), the whole list
1465
+ * each time, each marked `yours` for this runner's person, and the caps of asks.json that the
1466
+ * asker's runner checks over its person's questions on all their computers (`asking`): open
1467
+ * questions per person per expert, questions per person in the last day, and the hours after which
1468
+ * a question still unanswered ends. Sent to a runner that said `expertInstances` once its hello is
1469
+ * let in, even an empty one, and whenever it changes; the first on a connection opens the family on
1470
+ * it (see "Expert records and instances"). Each key once. `sharesWhatExpertInstancesLearn` (protocol
1471
+ * 0.18.0): this server takes and sends the messages of "What instances learn", said only to a runner
1472
+ * whose hello said it; the first directory on a connection decides it for that connection.
1473
+ */
1474
+ export const ExpertDirectoryMessageSchema = z
1475
+ .object({
1476
+ type: z.literal("expert-directory"),
1477
+ v: Version,
1478
+ experts: z.array(ExpertDirectoryEntrySchema).max(MAX_EXPERT_RECORDS),
1479
+ asking: z.object({
1480
+ openPerPersonPerExpert: z.number().int().positive(),
1481
+ perPersonPerDay: z.number().int().positive(),
1482
+ maxAgeHours: z.number().positive(),
1483
+ }),
1484
+ sharesWhatExpertInstancesLearn: z.literal(true).optional(),
1485
+ })
1486
+ .superRefine((message, context) => checkEachKeyOnce("experts", message.experts, context));
1487
+ /**
1488
+ * The expert record `expertRecordId` at `revision`, with its definition (protocol 0.17.0): in answer
1489
+ * to `expert-record-wanted`, and sent unasked when the revision changes to every linked runner whose
1490
+ * `expert-instance-states` has that record at `grounding` or `proof`, `ready`, or failed for a reason
1491
+ * other than `clone` or `key-used-here`; a runner still cloning asks once it has the project.
1492
+ * `owner` is the expert's owner, and `yours` marks it as this runner's person's own.
1493
+ */
1494
+ export const ExpertRecordMessageSchema = z.object({
1495
+ type: z.literal("expert-record"),
1496
+ v: Version,
1497
+ expertRecordId: ExpertRecordIdSchema,
1498
+ revision: Revision,
1499
+ owner: Account,
1500
+ yours: z.literal(true).optional(),
1501
+ definition: ExpertDefinitionSchema,
1502
+ });
1503
+ const expertDefinitionResult = { type: z.literal("expert-definition-result"), v: Version, key: ExpertNameSchema };
1504
+ const expertDefinitionRefused = { ...expertDefinitionResult, outcome: z.literal("refused") };
1505
+ /**
1506
+ * The answer to `expert-definition` for the expert `key` (protocol 0.17.0): `published`, as the record
1507
+ * `expertRecordId` at `revision` (a definition unchanged keeps its revision); or `refused`, for its
1508
+ * `reason`: `edited-elsewhere`, the record changed since the revision `basedOn` named, so the runner
1509
+ * writes the record's `definition`, at `revision`, into the expert file; `key-taken`, another
1510
+ * person's expert has the key; `not-owner`, the record `basedOn` named is now another person's, its
1511
+ * owner changed; `project-not-in-an-office`, no office on this server holds its project; `deleted`,
1512
+ * the record `basedOn` named was deleted on the floor.
1513
+ */
1514
+ export const ExpertDefinitionResultMessageSchema = z.discriminatedUnion("outcome", [
1515
+ z.object({ ...expertDefinitionResult, outcome: z.literal("published"), expertRecordId: ExpertRecordIdSchema, revision: Revision }),
1516
+ z.discriminatedUnion("reason", [
1517
+ z.object({ ...expertDefinitionRefused, reason: z.literal("edited-elsewhere"), expertRecordId: ExpertRecordIdSchema, revision: Revision, definition: ExpertDefinitionSchema }),
1518
+ z.object({ ...expertDefinitionRefused, reason: z.enum(["key-taken", "not-owner", "project-not-in-an-office", "deleted"]) }),
1519
+ ]),
1520
+ ]);
1521
+ /**
1522
+ * The expert record `expertRecordId` of the expert `key` was deleted on the floor, by `by` (protocol
1523
+ * 0.17.0): sent to every linked runner then, and to each runner that links within the 30 days after.
1524
+ * A runner acts on it only where its files are tied to that record, never another record of the key.
1525
+ */
1526
+ export const ExpertDeletedMessageSchema = z.object({
1527
+ type: z.literal("expert-deleted"),
1528
+ v: Version,
1529
+ key: ExpertNameSchema,
1530
+ expertRecordId: ExpertRecordIdSchema,
1531
+ by: Account,
1532
+ });
1533
+ /**
1534
+ * Set up this computer's instance of the expert `key` from the record `expertRecordId` (protocol
1535
+ * 0.17.0): its person's one click on the floor, or Try again after a failure (`again`, which grounds
1536
+ * it again now). Sent to that person's own runner on the computer they chose; `by` is the account
1537
+ * that runner said hello as, which the runner checks is its own, starting nothing otherwise. Nothing
1538
+ * is queued: the server refuses at once when that runner isn't linked. How the set-up goes, the
1539
+ * runner says in `expert-instance-states`.
1540
+ */
1541
+ export const SetUpExpertInstanceMessageSchema = z.object({
1542
+ type: z.literal("set-up-expert-instance"),
1543
+ v: Version,
1544
+ request: RequestId,
1545
+ key: ExpertNameSchema,
1546
+ expertRecordId: ExpertRecordIdSchema,
1547
+ by: Account,
1548
+ again: z.literal(true).optional(),
1549
+ });
1550
+ /**
1551
+ * The offer to set up the expert `key` on this computer, from the record `expertRecordId`, ended
1552
+ * (protocol 0.17.0): `dismissed` on the floor; `expired`; `set-up`, an instance of it is listed; or
1553
+ * `gone`, the record was deleted or its project left every office. Sent to that person's runner on
1554
+ * that computer when it ends, and on each link for every offer of that computer that ended in the 30
1555
+ * days after it was offered.
1556
+ */
1557
+ export const ExpertSetUpOfferEndedMessageSchema = z.object({
1558
+ type: z.literal("expert-set-up-offer-ended"),
1559
+ v: Version,
1560
+ key: ExpertNameSchema,
1561
+ expertRecordId: ExpertRecordIdSchema,
1562
+ why: z.enum(["dismissed", "expired", "set-up", "gone"]),
1563
+ });
1564
+ /**
1565
+ * This runner's person's questions to experts on their other computers (protocol 0.17.0), from the
1566
+ * records those computers' runners sent (`expert-question`): `open`, how many are open per expert,
1567
+ * and `askedAtInTheLastDay`, when each of the last day's was asked. The runner adds its own and
1568
+ * checks the directory's `asking` caps against the total. Sent after the directory on each link, and
1569
+ * whenever those records change.
1570
+ */
1571
+ export const ExpertQuestionsAskedOnYourOtherComputersMessageSchema = z.object({
1572
+ type: z.literal("expert-questions-asked-on-your-other-computers"),
1573
+ v: Version,
1574
+ open: z.array(z.object({ key: ExpertNameSchema, count: z.number().int().positive() })).max(MAX_EXPERT_RECORDS),
1575
+ askedAtInTheLastDay: z.array(z.iso.datetime({ offset: true })).max(MAX_EXPERT_QUESTION_TIMES_IN_THE_LAST_DAY),
1576
+ });
1577
+ const expertInstanceLearnedReceived = {
1578
+ type: z.literal("expert-instance-learned-received"),
1579
+ v: Version,
1580
+ key: ExpertNameSchema,
1581
+ expertRecordId: ExpertRecordIdSchema,
1582
+ id: WhatWasLearnedIdSchema,
1583
+ };
1584
+ /**
1585
+ * The answer to `expert-instance-learned`, by its `id` (protocol 0.18.0): `kept`, at `arrivalNumber`
1586
+ * in the record; `removed`, it was kept and removed on the card since; or `refused`, for its `reason`
1587
+ * (`not-an-instance-here`: the runner sends it again after its next listing, and on its timer). A
1588
+ * repeat of the same id gets the same answer. Only `kept` carries an arrival number.
1589
+ */
1590
+ export const ExpertInstanceLearnedReceivedMessageSchema = z.discriminatedUnion("outcome", [
1591
+ z.object({ ...expertInstanceLearnedReceived, outcome: z.literal("kept"), arrivalNumber: ArrivalNumber }),
1592
+ z.object({ ...expertInstanceLearnedReceived, outcome: z.literal("removed"), arrivalNumber: NoArrivalNumber }),
1593
+ z.object({ ...expertInstanceLearnedReceived, outcome: z.literal("refused"), reason: WhatWasLearnedRefusal, arrivalNumber: NoArrivalNumber }),
1594
+ ]);
1595
+ /**
1596
+ * Entries of the record `expertRecordId` after `afterArrivalNumber`, in arrival order (protocol
1597
+ * 0.18.0): in answer to `expert-learned-wanted-after`, in as many messages as it takes, each at most
1598
+ * MAX_WHAT_WAS_LEARNED_ENTRIES_PER_MESSAGE entries, each continuing from the one before, `more` on
1599
+ * all but the last; then pushed on that connection as entries arrive, each continuing from the last
1600
+ * sent. `chain` is the chain at its last entry, or at `afterArrivalNumber` when it carries none.
1601
+ * `lastArrivalNumber` is the server's last for the record, at or above every entry's. `startedAgain`,
1602
+ * on the first message of an answer only, which then starts from 0: the number asked after is beyond
1603
+ * the server's last, or the chain asked with isn't the server's at that number (a restored server).
1604
+ */
1605
+ export const ExpertLearnedSinceMessageSchema = z
1606
+ .object({
1607
+ type: z.literal("expert-learned-since"),
1608
+ v: Version,
1609
+ key: ExpertNameSchema,
1610
+ expertRecordId: ExpertRecordIdSchema,
1611
+ afterArrivalNumber: ArrivalNumberOrNone,
1612
+ entries: z.array(WhatWasLearnedEntrySchema).max(MAX_WHAT_WAS_LEARNED_ENTRIES_PER_MESSAGE),
1613
+ chain: ArrivalChain,
1614
+ lastArrivalNumber: ArrivalNumberOrNone,
1615
+ startedAgain: z.literal(true).optional(),
1616
+ more: z.literal(true).optional(),
1617
+ })
1618
+ .superRefine((message, context) => {
1619
+ let before = message.afterArrivalNumber;
1620
+ message.entries.forEach((entry, i) => {
1621
+ if (entry.arrivalNumber <= before)
1622
+ context.addIssue({ code: "custom", path: ["entries", i, "arrivalNumber"], message: `above ${before}: each entry's arrival number is above the one before it, the first above afterArrivalNumber` });
1623
+ if (entry.arrivalNumber > message.lastArrivalNumber)
1624
+ context.addIssue({ code: "custom", path: ["entries", i, "arrivalNumber"], message: `at most lastArrivalNumber, ${message.lastArrivalNumber}` });
1625
+ before = Math.max(before, entry.arrivalNumber);
1626
+ });
1627
+ if (message.startedAgain && message.afterArrivalNumber !== 0)
1628
+ context.addIssue({ code: "custom", path: ["startedAgain"], message: "only on an answer from 0: afterArrivalNumber 0" });
1629
+ });
1630
+ /**
1631
+ * The answer to an `expert-learned-wanted-after` the server can't take (protocol 0.18.0), so the
1632
+ * runner is never left waiting: `record-gone`, and the runner deletes what it has of that record;
1633
+ * `not-an-instance-here`, and it asks again after its next listing, and on its timer.
1634
+ */
1635
+ export const ExpertLearnedWantedAfterRefusedMessageSchema = z.object({
1636
+ type: z.literal("expert-learned-wanted-after-refused"),
1637
+ v: Version,
1638
+ key: ExpertNameSchema,
1639
+ expertRecordId: ExpertRecordIdSchema,
1640
+ reason: WhatWasLearnedRefusal,
1641
+ });
1238
1642
  export const OfficeServerMessageSchema = z.discriminatedUnion("type", [
1239
1643
  RepairMessageSchema,
1240
1644
  WelcomeMessageSchema,
@@ -1251,11 +1655,20 @@ export const OfficeServerMessageSchema = z.discriminatedUnion("type", [
1251
1655
  DriveMessageSchema,
1252
1656
  DrivePolicyMessageSchema,
1253
1657
  ExpertRightsMessageSchema,
1254
- HostingMessageSchema,
1255
1658
  CreateExpertFromSuggestionMessageSchema,
1256
1659
  DraftOfficeWideExpertCreationSuggestionMessageSchema,
1257
1660
  ExpertCreationSuggestionDismissalsMessageSchema,
1258
1661
  ServerExpertCreationSuggestionEndedMessageSchema,
1662
+ ExpertDirectoryMessageSchema,
1663
+ ExpertRecordMessageSchema,
1664
+ ExpertDefinitionResultMessageSchema,
1665
+ ExpertDeletedMessageSchema,
1666
+ SetUpExpertInstanceMessageSchema,
1667
+ ExpertSetUpOfferEndedMessageSchema,
1668
+ ExpertQuestionsAskedOnYourOtherComputersMessageSchema,
1669
+ ExpertInstanceLearnedReceivedMessageSchema,
1670
+ ExpertLearnedSinceMessageSchema,
1671
+ ExpertLearnedWantedAfterRefusedMessageSchema,
1259
1672
  ]);
1260
1673
  /** Validates a message an office server received from a data layer. */
1261
1674
  export function parseDataLayerMessage(value) {