@isoplan/protocol 0.15.0 → 0.17.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,10 @@ 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.
204
208
  */
205
209
  export const RunnerHelloMessageSchema = z.object({
206
210
  type: z.literal("runner"),
@@ -213,9 +217,9 @@ export const RunnerHelloMessageSchema = z.object({
213
217
  drive: z.boolean().optional(),
214
218
  claude: ClaudeCliSchema.optional(),
215
219
  starts: z.array(StartModeSchema).max(4).optional(),
216
- experts: z.literal(true).optional(),
217
220
  hiddenStarts: z.literal(true).optional(),
218
221
  canCreateExpertsFromSuggestions: z.literal(true).optional(),
222
+ expertInstances: z.literal(true).optional(),
219
223
  });
220
224
  /** All of the session's facts. Sent on connect and on repair. */
221
225
  export const FullMessageSchema = z.object({
@@ -243,11 +247,11 @@ export const MAX_OWNER_MESSAGE_LENGTH = 20000;
243
247
  * the plugin in the session never said whether it took the message (the session may have closed).
244
248
  */
245
249
  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). */
250
+ // Experts (protocol 0.13.0): read-only agents, each grounded in one repo, which any session in an
251
+ // office that holds that repo may ask (ask_expert). From 0.17.0 each computer keeps its own instance
252
+ // of an expert, set up from its expert record, and answers its own sessions' questions from it (see
253
+ // "Expert records and instances"). These shared pieces come before the messages that use them.
254
+ /** The most experts a session is told it may ask (protocol 0.13.0). */
251
255
  export const MAX_EXPERTS = 20;
252
256
  /** The longest subject an expert lists: what to ask it about, and before which changes. */
253
257
  export const MAX_EXPERT_SUBJECT_LENGTH = 1000;
@@ -261,22 +265,23 @@ export const ExpertNameSchema = z.string().regex(/^[a-z0-9][a-z0-9-]{0,62}$/, "l
261
265
  /** A full commit hash, as a handoff repo's. */
262
266
  const Commit = z.string().regex(/^([0-9a-f]{40}|[0-9a-f]{64})$/, "a full commit hash");
263
267
  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. */
268
+ /**
269
+ * Every situation ask_expert tells its agent about, by a stable code. `EXPERT_NOT_SET_UP` (protocol
270
+ * 0.17.0): this computer has no instance of the expert yet, so its set-up is offered. No 0.17.0 side
271
+ * sends `EXPERT_QUEUED` or `EXPERT_DROPPED`; an office from before 0.17.0 may still send them in a
272
+ * late end. `EXPERT_QUESTION_HELD` is gone (0.17.0): a runner made it, and no office ever sent it.
273
+ */
265
274
  export const ExpertCodeSchema = z.enum([
266
275
  "EXPERT_RUNNER_UNREACHABLE",
267
276
  "EXPERT_UNKNOWN",
268
277
  "EXPERT_CAP",
269
- "EXPERT_QUESTION_HELD",
270
278
  "EXPERT_PLUGIN_OLD",
279
+ "EXPERT_NOT_SET_UP",
271
280
  "EXPERT_QUEUED",
272
281
  "EXPERT_STARTING",
273
282
  "EXPERT_FORK_FAILED",
274
283
  "EXPERT_DROPPED",
275
284
  ]);
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
285
  /** The most folders or globs an expert's area names (protocol 0.15.0). */
281
286
  export const MAX_AREA_PATHS = 20;
282
287
  /**
@@ -285,32 +290,11 @@ export const MAX_AREA_PATHS = 20;
285
290
  */
286
291
  export const expertDisplayName = (key) => [...key.split("-").filter(Boolean).map((word) => word[0].toUpperCase() + word.slice(1)), "Expert"].join(" ");
287
292
  /**
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.
293
+ * One expert a session may ask: `repo` is its project's short name, `host` a person's name: the
294
+ * hosting person's until 0.17.0, and from 0.17.0 the expert's owner's, kept so an older plugin still
295
+ * parses it.
293
296
  */
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
297
  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
298
  /** 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
299
  export const CapSchema = z.enum(["room-hour", "owner-day", "owner-tokens"]);
316
300
  /** The answer to a request, carrying the request's id and kind. */
@@ -331,9 +315,6 @@ export const ReplyMessageSchema = z.discriminatedUnion("kind", [
331
315
  z.object({ type: z.literal("reply"), v: Version, id: RequestId, kind: z.literal("owner-message"), queued: z.literal(true).optional() }),
332
316
  // The handoff's session is in front in Orca now (protocol 0.3.8); one that couldn't be is refused `unavailable`.
333
317
  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
318
  ]);
338
319
  /**
339
320
  * A request the machine won't answer, sent in place of its reply. `over-cap` names the cap it would
@@ -364,9 +345,7 @@ export const RefusalMessageSchema = z.discriminatedUnion("reason", [
364
345
  unchecked: z.array(z.string().min(1)).optional(),
365
346
  }),
366
347
  // `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() }),
348
+ z.object({ type: z.literal("refusal"), v: Version, id: RequestId, reason: z.literal("unavailable"), detail: z.string().min(1), code: OwnerMessageRefusalSchema.optional() }),
370
349
  ]);
371
350
  /**
372
351
  * The sender's session handed its room over with the command: its agent wrote the brief and pushed.
@@ -443,41 +422,6 @@ export const AskedAckMessageSchema = z.object({
443
422
  v: Version,
444
423
  question: RequestId,
445
424
  });
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
425
  /**
482
426
  * A session asks an expert a question (protocol 0.13.0), on its own short link with the session's
483
427
  * hello, as ask-room is. `key` is the tool call's id: asked again under it, it is the same question.
@@ -486,6 +430,8 @@ export const ExpertSeniorMessageSchema = z.object({
486
430
  * pairing codes, as for ask-room). It is a message of its own, not an ask-room with an expert, so a
487
431
  * server from before experts refuses it rather than asking a room. The session then polls it with
488
432
  * ask-check and acks its end with asked-ack, as for a room question.
433
+ * From 0.17.0 a server answers it only with an error `EXPERT_PLUGIN_OLD`, and a 0.17.0 plugin never
434
+ * sends it: it asks its own computer's instance of the expert (see `expert-question`).
489
435
  */
490
436
  export const AskExpertMessageSchema = z.object({
491
437
  type: z.literal("ask-expert"),
@@ -546,10 +492,17 @@ const Area = z.array(AreaPathSchema).min(1).max(MAX_AREA_PATHS);
546
492
  * and `proof`, as the runner's local model wrote them and the secret check passed them. `why`, the
547
493
  * signals that passed their thresholds, each other one 0: `share`, the share of the session's context
548
494
  * 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.
495
+ * `minutes`, the time spent there; `corrections`, the person's corrections there. `repetition`
496
+ * (from 0.16.0, a session's suggestion only), what was done in its area again, which suggested it:
497
+ * its `folder`, the file read most, was read in `readings` turns over `sessions` sessions, the
498
+ * area's `files` read again with it; or its folder was searched `searches` times. An older runner
499
+ * sends none, and an older side ignores it. `at`: when it was suggested. Of its text, only these
500
+ * leave the machine, under ADR 0004's case for an expert creation suggestion's draft.
552
501
  */
502
+ export const ExpertCreationSuggestionRepetitionSchema = z.union([
503
+ z.object({ readings: z.number().int().min(2), sessions: z.number().int().positive(), files: z.number().int().positive() }),
504
+ z.object({ searches: z.number().int().positive() }),
505
+ ]);
553
506
  export const ExpertCreationSuggestionMessageSchema = z.object({
554
507
  type: z.literal("expert-creation-suggestion"),
555
508
  v: Version,
@@ -568,6 +521,7 @@ export const ExpertCreationSuggestionMessageSchema = z.object({
568
521
  minutes: z.number().nonnegative(),
569
522
  corrections: z.number().int().nonnegative(),
570
523
  }),
524
+ repetition: ExpertCreationSuggestionRepetitionSchema.optional(),
571
525
  at: z.iso.datetime({ offset: true }),
572
526
  });
573
527
  /**
@@ -600,6 +554,190 @@ export const ExpertCreationProgressMessageSchema = z.object({
600
554
  state: ExpertCreationProgressStateSchema,
601
555
  detail: Detail.optional(),
602
556
  });
557
+ // Expert records and instances (protocol 0.17.0, isoplan-app#340): an expert's definition is kept in
558
+ // its expert record on the office server, which its owner's runner publishes from the expert file
559
+ // (`expert-definition`). Every computer whose sessions ask the expert keeps its own instance of it,
560
+ // set up from the record, and answers those questions itself: neither a question nor its answer
561
+ // leaves the computer, and the server is told only a record of each with no words
562
+ // (`expert-question`). A runner that can says `expertInstances` in its hello. The server sends such
563
+ // a runner `expert-directory` once that hello is let in, even an empty one, and whenever it changes,
564
+ // and sends none of the family to a runner that didn't say it. The runner sends nothing of the
565
+ // family on a connection before that connection's first `expert-directory`, so a server from before
566
+ // 0.17.0, which sends none, is never sent any. A runner sends the family only while its link is up,
567
+ // and counts a message sent only once it went: one that didn't, and one it must send again on a new
568
+ // connection, goes after that connection's first `expert-directory`. The server takes a repeat as
569
+ // the same message, so sending one again is always safe.
570
+ // Ownership is told, never worked out on the runner: the server marks what is this runner's
571
+ // person's own with `yours`, and names a person to the runner as the account that runner said hello
572
+ // as (`by`), since with sign-in on the server's id for a person isn't the Claude account the runner
573
+ // knows itself by.
574
+ /** The most expert records an `expert-directory` or an `expert-instance-states` names. */
575
+ export const MAX_EXPERT_RECORDS = 1000;
576
+ /** The most times an `expert-questions-asked-on-your-other-computers` lists for the last day. */
577
+ export const MAX_EXPERT_QUESTION_TIMES_IN_THE_LAST_DAY = 1000;
578
+ /** A definition's revision in its expert record: 1 when the record is made, one more at each change. */
579
+ const Revision = z.number().int().positive();
580
+ /**
581
+ * Which expert record: made by the server when the record is made, so a record made again under the
582
+ * key of a deleted one is told apart from the old one.
583
+ */
584
+ export const ExpertRecordIdSchema = z.uuid();
585
+ /** The record and revision a computer last had of the definition it publishes. */
586
+ const BasedOn = z.object({ expertRecordId: ExpertRecordIdSchema, revision: Revision });
587
+ /**
588
+ * What every computer's instance of an expert shares, as its owner's expert file says it: its key;
589
+ * its project, one with a remote, never a folder on one computer (`local:`); its subject; the model
590
+ * it grounds with; the context, in tokens, at which its junior is promoted (`promoteAtTokens`); the
591
+ * folders and globs of its repo it covers (`areas`); and its proof question with the answer expected.
592
+ * The rest of an expert file is that computer's own, and never leaves it.
593
+ */
594
+ export const ExpertDefinitionSchema = z.object({
595
+ key: ExpertNameSchema,
596
+ 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"),
597
+ subject: Subject,
598
+ model: Model,
599
+ promoteAtTokens: z.number().int().positive(),
600
+ areas: z.array(AreaPathSchema).max(MAX_AREA_PATHS),
601
+ proof: Proof,
602
+ });
603
+ /**
604
+ * Where this computer's instance of the expert `key` stands, set up from (or, on its owner's
605
+ * computer, published as) the record `expertRecordId`: `setting-up`, with its `step` (`cloning`,
606
+ * finding this computer's checkout of the project or cloning it; `grounding`; `proof`); `ready`, its
607
+ * junior answers; or `failed`, with its `failure`: `clone`, the project couldn't be cloned, with
608
+ * git's words in `detail`, secret-checked on this computer; `proof`, its proof question failed;
609
+ * `key-used-here`, this computer has its own expert with that key, whose publish was refused
610
+ * `key-taken`, so the record can't be set up here until that one is renamed; `other`, with why in
611
+ * `detail`. `senior`: it has a senior generation, which its junior can ask. `groundingTokens`: its
612
+ * junior's context after grounding. No definition field: the record has it.
613
+ */
614
+ export const ExpertInstanceStateSchema = z.object({
615
+ key: ExpertNameSchema,
616
+ expertRecordId: ExpertRecordIdSchema,
617
+ state: z.enum(["setting-up", "ready", "failed"]),
618
+ step: z.enum(["cloning", "grounding", "proof"]).optional(),
619
+ senior: z.literal(true).optional(),
620
+ groundingTokens: z.number().int().positive().optional(),
621
+ failure: z.object({ why: z.enum(["clone", "proof", "key-used-here", "other"]), detail: Detail.optional() }).optional(),
622
+ });
623
+ /**
624
+ * One expert record in an `expert-directory`: one whose project an office holds. `owner` is the
625
+ * expert's owner, `yours` marks a record this runner's person owns, `revision` is its definition's
626
+ * revision now, and `groundingTokens` a junior's context after grounding, as an instance last said.
627
+ */
628
+ export const ExpertDirectoryEntrySchema = z.object({
629
+ key: ExpertNameSchema,
630
+ expertRecordId: ExpertRecordIdSchema,
631
+ project: ProjectSchema,
632
+ owner: Account,
633
+ yours: z.literal(true).optional(),
634
+ revision: Revision,
635
+ groundingTokens: z.number().int().positive().optional(),
636
+ });
637
+ /** Adds an issue for each entry of the list `field` that names a key an earlier entry named. */
638
+ const checkEachKeyOnce = (field, entries, context) => {
639
+ const seen = new Set();
640
+ entries.forEach((entry, i) => {
641
+ if (seen.has(entry.key))
642
+ context.addIssue({ code: "custom", path: [field, i, "key"], message: `the key ${entry.key} is named once only` });
643
+ seen.add(entry.key);
644
+ });
645
+ };
646
+ /**
647
+ * The owner's runner publishes the expert's definition from the expert file (protocol 0.17.0), after
648
+ * checking it for secrets on this computer (a match sends nothing). `basedOn`: the record and
649
+ * revision this computer last had of it from this server; absent for a file never published there.
650
+ * Sent on each new connection while the file differs from that revision, and on each change. The
651
+ * server answers `expert-definition-result`; the same definition and `basedOn` again get the same
652
+ * result.
653
+ */
654
+ export const ExpertDefinitionMessageSchema = z.object({
655
+ type: z.literal("expert-definition"),
656
+ v: Version,
657
+ definition: ExpertDefinitionSchema,
658
+ basedOn: BasedOn.optional(),
659
+ });
660
+ /**
661
+ * Send this computer the record `expertRecordId` of the expert `key` (protocol 0.17.0), answered
662
+ * with `expert-record`. Sent only once this computer has a checkout or clone of the record's
663
+ * project, and again after each new connection's first `expert-directory` while it is still wanted.
664
+ */
665
+ export const ExpertRecordWantedMessageSchema = z.object({
666
+ type: z.literal("expert-record-wanted"),
667
+ v: Version,
668
+ key: ExpertNameSchema,
669
+ expertRecordId: ExpertRecordIdSchema,
670
+ });
671
+ /**
672
+ * Every instance on this computer of this server's expert records whose project this server takes
673
+ * (protocol 0.17.0), the whole list each time: the first on each connection always, even an empty
674
+ * one, then only when it differs. An instance set up from one office server is never named to
675
+ * another. Each key once.
676
+ */
677
+ export const ExpertInstanceStatesMessageSchema = z
678
+ .object({
679
+ type: z.literal("expert-instance-states"),
680
+ v: Version,
681
+ instances: z.array(ExpertInstanceStateSchema).max(MAX_EXPERT_RECORDS),
682
+ })
683
+ .superRefine((message, context) => checkEachKeyOnce("instances", message.instances, context));
684
+ /**
685
+ * A question this computer's session asked the expert `key` (protocol 0.17.0), as a record with no
686
+ * words: never the question's or the answer's text. `id` is the tool call's id, `session` the asking
687
+ * session as its hello names it (`claude-code:<id>`), `blocking` whether its agent waited, `state`
688
+ * where it stands (`asking`, `answered` or `dropped`, with its `code`), `senior` that its junior asked
689
+ * its senior, and `askedAt` and `endedAt` when it was asked and ended. Sent at each change of state,
690
+ * and again after each new connection's first `expert-directory` until its end has gone. The same
691
+ * `id` and `state` again is the same record.
692
+ */
693
+ export const ExpertQuestionMessageSchema = z.object({
694
+ type: z.literal("expert-question"),
695
+ v: Version,
696
+ id: z.string().min(1).max(100),
697
+ key: ExpertNameSchema,
698
+ expertRecordId: ExpertRecordIdSchema,
699
+ session: z.string().min(1),
700
+ blocking: z.boolean(),
701
+ state: z.enum(["asking", "answered", "dropped"]),
702
+ code: ExpertCodeSchema.optional(),
703
+ senior: z.literal(true).optional(),
704
+ askedAt: z.iso.datetime({ offset: true }),
705
+ endedAt: z.iso.datetime({ offset: true }).optional(),
706
+ });
707
+ /**
708
+ * An agent on this computer asked the expert `key`, of which this computer has no instance, so its
709
+ * person is offered to set it up (protocol 0.17.0, ADR 0013). Sent once per computer per record,
710
+ * ever, and again after each new connection's first `expert-directory` until it has gone. A repeat
711
+ * for the same record, person and computer changes nothing. No text of the question.
712
+ */
713
+ export const ExpertSetUpOfferedMessageSchema = z.object({
714
+ type: z.literal("expert-set-up-offered"),
715
+ v: Version,
716
+ key: ExpertNameSchema,
717
+ expertRecordId: ExpertRecordIdSchema,
718
+ });
719
+ /**
720
+ * This computer's instance of the expert `key`, whose record isn't its person's own, failed its proof
721
+ * question at `revision` until its tries ran out (protocol 0.17.0), for the expert's owner to see:
722
+ * `answer`, what its fork answered, secret-checked on this computer, or `answerHeld`, that it matched
723
+ * a secret pattern and so isn't sent. Exactly one of the two. Sent once per revision, and again after
724
+ * each new connection's first `expert-directory` until it has gone. A repeat for the same record,
725
+ * computer and revision changes nothing.
726
+ */
727
+ export const ExpertProofFailedMessageSchema = z
728
+ .object({
729
+ type: z.literal("expert-proof-failed"),
730
+ v: Version,
731
+ key: ExpertNameSchema,
732
+ expertRecordId: ExpertRecordIdSchema,
733
+ revision: Revision,
734
+ answer: z.string().min(1).max(MAX_EXPERT_ANSWER_LENGTH).optional(),
735
+ answerHeld: z.literal(true).optional(),
736
+ })
737
+ .superRefine((message, context) => {
738
+ if ((message.answer === undefined) === (message.answerHeld === undefined))
739
+ context.addIssue({ code: "custom", path: ["answer"], message: "the fork's answer, or answerHeld: one of the two" });
740
+ });
603
741
  const driven = { type: z.literal("driven"), v: Version };
604
742
  /**
605
743
  * Why a runner won't do what a `drive` request asked, sent in place of its answer (protocol 0.5.0).
@@ -768,12 +906,16 @@ export const DataLayerMessageSchema = z.discriminatedUnion("type", [
768
906
  AskCheckMessageSchema,
769
907
  AskedAckMessageSchema,
770
908
  DrivenMessageSchema,
771
- ExpertsMessageSchema,
772
909
  AskExpertMessageSchema,
773
- ExpertSeniorMessageSchema,
774
910
  ExpertCreationSuggestionMessageSchema,
775
911
  RunnerExpertCreationSuggestionEndedMessageSchema,
776
912
  ExpertCreationProgressMessageSchema,
913
+ ExpertDefinitionMessageSchema,
914
+ ExpertRecordWantedMessageSchema,
915
+ ExpertInstanceStatesMessageSchema,
916
+ ExpertQuestionMessageSchema,
917
+ ExpertSetUpOfferedMessageSchema,
918
+ ExpertProofFailedMessageSchema,
777
919
  ]);
778
920
  // Office server → data layer.
779
921
  /** Asks the data layer to send its facts in full. */
@@ -880,22 +1022,6 @@ export const RequestMessageSchema = z.discriminatedUnion("kind", [
880
1022
  kind: z.literal("focus"),
881
1023
  handoff: HandoffId,
882
1024
  }),
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
1025
  ]);
900
1026
  /**
901
1027
  * The request is over (its handoff was cancelled, dismissed or replaced): the machine drops what it
@@ -974,7 +1100,8 @@ export const HandedOverMessageSchema = z.object({
974
1100
  * An answered or dropped question is sent again down the session's own link until it is acked.
975
1101
  * A question asked of an expert (protocol 0.13.0) has `expert`, and `owner` is the host's name; its
976
1102
  * `code` says where it stands for ask_expert, and an answered one has the `commit` and `branch` its
977
- * answer describes.
1103
+ * answer describes. From 0.17.0 no office sends one for an expert's question, which the asker's own
1104
+ * runner answers, though an office from before 0.17.0 may still send a late end.
978
1105
  */
979
1106
  export const AskedMessageSchema = z.object({
980
1107
  type: z.literal("asked"),
@@ -1014,21 +1141,6 @@ export const ExpertRightsMessageSchema = z.object({
1014
1141
  v: Version,
1015
1142
  experts: z.array(ExpertRightSchema).max(MAX_EXPERTS),
1016
1143
  });
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
1144
  const drive = { type: z.literal("drive"), v: Version, id: DriveId, by: Account };
1033
1145
  /**
1034
1146
  * A person acting on a driven session from the floor (protocol 0.5.0), sent only to a runner that said
@@ -1040,8 +1152,9 @@ const drive = { type: z.literal("drive"), v: Version, id: DriveId, by: Account }
1040
1152
  export const DriveMessageSchema = z.discriminatedUnion("kind", [
1041
1153
  // Start a driven session for `room`, a room of `project` that `by` is in. It opens in the folder of
1042
1154
  // the first of `roomSessions` (the room's sessions on this machine, newest first) that the runner
1043
- // reports, by its own record of that folder, or else in a checkout of `project` it finds, as a take
1044
- // does. `instruction`, the first, is the turn named by this request's id. `permissions` is the mode
1155
+ // has its own record of, still there and in a repo the office takes. With none, the start is
1156
+ // refused `no-folder`: the runner doesn't look for some other checkout of `project`, as a take does.
1157
+ // `instruction`, the first, is the turn named by this request's id. `permissions` is the mode
1045
1158
  // the person asked for; the runner runs `bypassPermissions` only where `drive-policy` allows it.
1046
1159
  // Or, with `folder` and no `room` or `roomSessions` (#67): a fresh session in a new room, in one of
1047
1160
  // the folders the runner's last `folders` listed for `project`, with nothing from any room passed
@@ -1234,6 +1347,117 @@ export const ServerExpertCreationSuggestionEndedMessageSchema = z.discriminatedU
1234
1347
  z.object({ ...expertCreationSuggestionEnded, why: z.literal("made"), key: ExpertNameSchema }),
1235
1348
  z.object({ ...expertCreationSuggestionEnded, why: z.enum(["dismissed", "refused", "expired"]) }),
1236
1349
  ]);
1350
+ /**
1351
+ * Every expert record whose project an office on this server holds (protocol 0.17.0), the whole list
1352
+ * each time, each marked `yours` for this runner's person, and the caps of asks.json that the
1353
+ * asker's runner checks over its person's questions on all their computers (`asking`): open
1354
+ * questions per person per expert, questions per person in the last day, and the hours after which
1355
+ * a question still unanswered ends. Sent to a runner that said `expertInstances` once its hello is
1356
+ * let in, even an empty one, and whenever it changes; the first on a connection opens the family on
1357
+ * it (see "Expert records and instances"). Each key once.
1358
+ */
1359
+ export const ExpertDirectoryMessageSchema = z
1360
+ .object({
1361
+ type: z.literal("expert-directory"),
1362
+ v: Version,
1363
+ experts: z.array(ExpertDirectoryEntrySchema).max(MAX_EXPERT_RECORDS),
1364
+ asking: z.object({
1365
+ openPerPersonPerExpert: z.number().int().positive(),
1366
+ perPersonPerDay: z.number().int().positive(),
1367
+ maxAgeHours: z.number().positive(),
1368
+ }),
1369
+ })
1370
+ .superRefine((message, context) => checkEachKeyOnce("experts", message.experts, context));
1371
+ /**
1372
+ * The expert record `expertRecordId` at `revision`, with its definition (protocol 0.17.0): in answer
1373
+ * to `expert-record-wanted`, and sent unasked when the revision changes to every linked runner whose
1374
+ * `expert-instance-states` has that record at `grounding` or `proof`, `ready`, or failed for a reason
1375
+ * other than `clone` or `key-used-here`; a runner still cloning asks once it has the project.
1376
+ * `owner` is the expert's owner, and `yours` marks it as this runner's person's own.
1377
+ */
1378
+ export const ExpertRecordMessageSchema = z.object({
1379
+ type: z.literal("expert-record"),
1380
+ v: Version,
1381
+ expertRecordId: ExpertRecordIdSchema,
1382
+ revision: Revision,
1383
+ owner: Account,
1384
+ yours: z.literal(true).optional(),
1385
+ definition: ExpertDefinitionSchema,
1386
+ });
1387
+ const expertDefinitionResult = { type: z.literal("expert-definition-result"), v: Version, key: ExpertNameSchema };
1388
+ const expertDefinitionRefused = { ...expertDefinitionResult, outcome: z.literal("refused") };
1389
+ /**
1390
+ * The answer to `expert-definition` for the expert `key` (protocol 0.17.0): `published`, as the record
1391
+ * `expertRecordId` at `revision` (a definition unchanged keeps its revision); or `refused`, for its
1392
+ * `reason`: `edited-elsewhere`, the record changed since the revision `basedOn` named, so the runner
1393
+ * writes the record's `definition`, at `revision`, into the expert file; `key-taken`, another
1394
+ * person's expert has the key; `not-owner`, the record `basedOn` named is now another person's, its
1395
+ * owner changed; `project-not-in-an-office`, no office on this server holds its project; `deleted`,
1396
+ * the record `basedOn` named was deleted on the floor.
1397
+ */
1398
+ export const ExpertDefinitionResultMessageSchema = z.discriminatedUnion("outcome", [
1399
+ z.object({ ...expertDefinitionResult, outcome: z.literal("published"), expertRecordId: ExpertRecordIdSchema, revision: Revision }),
1400
+ z.discriminatedUnion("reason", [
1401
+ z.object({ ...expertDefinitionRefused, reason: z.literal("edited-elsewhere"), expertRecordId: ExpertRecordIdSchema, revision: Revision, definition: ExpertDefinitionSchema }),
1402
+ z.object({ ...expertDefinitionRefused, reason: z.enum(["key-taken", "not-owner", "project-not-in-an-office", "deleted"]) }),
1403
+ ]),
1404
+ ]);
1405
+ /**
1406
+ * The expert record `expertRecordId` of the expert `key` was deleted on the floor, by `by` (protocol
1407
+ * 0.17.0): sent to every linked runner then, and to each runner that links within the 30 days after.
1408
+ * A runner acts on it only where its files are tied to that record, never another record of the key.
1409
+ */
1410
+ export const ExpertDeletedMessageSchema = z.object({
1411
+ type: z.literal("expert-deleted"),
1412
+ v: Version,
1413
+ key: ExpertNameSchema,
1414
+ expertRecordId: ExpertRecordIdSchema,
1415
+ by: Account,
1416
+ });
1417
+ /**
1418
+ * Set up this computer's instance of the expert `key` from the record `expertRecordId` (protocol
1419
+ * 0.17.0): its person's one click on the floor, or Try again after a failure (`again`, which grounds
1420
+ * it again now). Sent to that person's own runner on the computer they chose; `by` is the account
1421
+ * that runner said hello as, which the runner checks is its own, starting nothing otherwise. Nothing
1422
+ * is queued: the server refuses at once when that runner isn't linked. How the set-up goes, the
1423
+ * runner says in `expert-instance-states`.
1424
+ */
1425
+ export const SetUpExpertInstanceMessageSchema = z.object({
1426
+ type: z.literal("set-up-expert-instance"),
1427
+ v: Version,
1428
+ request: RequestId,
1429
+ key: ExpertNameSchema,
1430
+ expertRecordId: ExpertRecordIdSchema,
1431
+ by: Account,
1432
+ again: z.literal(true).optional(),
1433
+ });
1434
+ /**
1435
+ * The offer to set up the expert `key` on this computer, from the record `expertRecordId`, ended
1436
+ * (protocol 0.17.0): `dismissed` on the floor; `expired`; `set-up`, an instance of it is listed; or
1437
+ * `gone`, the record was deleted or its project left every office. Sent to that person's runner on
1438
+ * that computer when it ends, and on each link for every offer of that computer that ended in the 30
1439
+ * days after it was offered.
1440
+ */
1441
+ export const ExpertSetUpOfferEndedMessageSchema = z.object({
1442
+ type: z.literal("expert-set-up-offer-ended"),
1443
+ v: Version,
1444
+ key: ExpertNameSchema,
1445
+ expertRecordId: ExpertRecordIdSchema,
1446
+ why: z.enum(["dismissed", "expired", "set-up", "gone"]),
1447
+ });
1448
+ /**
1449
+ * This runner's person's questions to experts on their other computers (protocol 0.17.0), from the
1450
+ * records those computers' runners sent (`expert-question`): `open`, how many are open per expert,
1451
+ * and `askedAtInTheLastDay`, when each of the last day's was asked. The runner adds its own and
1452
+ * checks the directory's `asking` caps against the total. Sent after the directory on each link, and
1453
+ * whenever those records change.
1454
+ */
1455
+ export const ExpertQuestionsAskedOnYourOtherComputersMessageSchema = z.object({
1456
+ type: z.literal("expert-questions-asked-on-your-other-computers"),
1457
+ v: Version,
1458
+ open: z.array(z.object({ key: ExpertNameSchema, count: z.number().int().positive() })).max(MAX_EXPERT_RECORDS),
1459
+ askedAtInTheLastDay: z.array(z.iso.datetime({ offset: true })).max(MAX_EXPERT_QUESTION_TIMES_IN_THE_LAST_DAY),
1460
+ });
1237
1461
  export const OfficeServerMessageSchema = z.discriminatedUnion("type", [
1238
1462
  RepairMessageSchema,
1239
1463
  WelcomeMessageSchema,
@@ -1250,11 +1474,17 @@ export const OfficeServerMessageSchema = z.discriminatedUnion("type", [
1250
1474
  DriveMessageSchema,
1251
1475
  DrivePolicyMessageSchema,
1252
1476
  ExpertRightsMessageSchema,
1253
- HostingMessageSchema,
1254
1477
  CreateExpertFromSuggestionMessageSchema,
1255
1478
  DraftOfficeWideExpertCreationSuggestionMessageSchema,
1256
1479
  ExpertCreationSuggestionDismissalsMessageSchema,
1257
1480
  ServerExpertCreationSuggestionEndedMessageSchema,
1481
+ ExpertDirectoryMessageSchema,
1482
+ ExpertRecordMessageSchema,
1483
+ ExpertDefinitionResultMessageSchema,
1484
+ ExpertDeletedMessageSchema,
1485
+ SetUpExpertInstanceMessageSchema,
1486
+ ExpertSetUpOfferEndedMessageSchema,
1487
+ ExpertQuestionsAskedOnYourOtherComputersMessageSchema,
1258
1488
  ]);
1259
1489
  /** Validates a message an office server received from a data layer. */
1260
1490
  export function parseDataLayerMessage(value) {