@biolibs/bionet 1.2.0 → 1.3.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/src/merge.ts CHANGED
@@ -26,7 +26,7 @@
26
26
  */
27
27
  import { Dseqrecord } from "@biolibs/dseqrecord";
28
28
  import type { SignOptions, SigningKey } from "@biolibs/opencloning";
29
- import { entryId, readEntry, verifyEntry, writeEntry, type Operation, type Role,
29
+ import { entryId, readEntry, verifyEntry, writeEntry, type Operation, type Role, type StandingMm,
30
30
  type SignedEntry } from "./log.js";
31
31
  import { INVENTORY_VERSION, type InventoryDocument, type InventoryPart, type LabModel,
32
32
  type PeerModel } from "./model.js";
@@ -84,12 +84,51 @@ export interface ItemState {
84
84
  retired: boolean;
85
85
  /** Whether the log has lost track: the amount went below zero, or two recounts overlapped. */
86
86
  needsRecount: boolean;
87
+ /** Where it is, if anybody has said: the place, and the position if the place is a box. */
88
+ where: { place: string; slot: { row: number; column: number } | null } | null;
87
89
  /** Anything else set on it. */
88
90
  fields: Record<string, string | number | null>;
89
91
  /** The entry that added it. */
90
92
  added: string;
91
93
  }
92
94
 
95
+ /** One place: a room, a freezer, a shelf, a box. */
96
+ export interface PlaceState {
97
+ /** Its ID. */
98
+ place: string;
99
+ /** What kind of place it is. Anything a lab calls one; the usual ones are drawn knowingly. */
100
+ kind: string;
101
+ /** What the lab calls it. */
102
+ name: string;
103
+ /** The place it is in, or `null` if it is in nothing. */
104
+ parent: string | null;
105
+ /** Its positions, if it is a box. Fixed when it is added. */
106
+ grid: { rows: number; columns: number } | null;
107
+ /** Where it stands and how big it is, if anybody has said. */
108
+ standing: StandingMm | null;
109
+ /** Whether it has been retired. */
110
+ retired: boolean;
111
+ /**
112
+ * Whether moves made apart left it inside itself. Such a place is shown as being in nothing
113
+ * until somebody moves it.
114
+ */
115
+ adrift: boolean;
116
+ /** Anything else set on it. */
117
+ fields: Record<string, string | number | null>;
118
+ /** The entry that added it. */
119
+ added: string;
120
+ }
121
+
122
+ /** One position of one box that the log says holds more than one item. */
123
+ export interface Crowded {
124
+ /** The box. */
125
+ place: string;
126
+ /** The position. */
127
+ slot: { row: number; column: number };
128
+ /** The items the log has there. */
129
+ items: string[];
130
+ }
131
+
93
132
  /** What became of one entry. */
94
133
  export type EntryStatus = "applied" | "set aside" | "refused" | "not understood";
95
134
 
@@ -135,6 +174,10 @@ export interface LabState {
135
174
  parts: PartState[];
136
175
  /** Every item. */
137
176
  items: ItemState[];
177
+ /** Every place. */
178
+ places: PlaceState[];
179
+ /** Positions the log says hold more than one item. Somebody has to go and look. */
180
+ crowded: Crowded[];
138
181
  /** Every entry that could be placed, in order, with what became of it. */
139
182
  entries: Placed[];
140
183
  /** Entries that are waiting on others that have not arrived. */
@@ -168,6 +211,31 @@ const RESERVED = new Set(["amount", "part", "item", "checksum", "sequence", "cir
168
211
  const text = (value: unknown, most: number): value is string =>
169
212
  typeof value === "string" && value.length > 0 && value.length <= most;
170
213
  /** An amount is a whole number of the item's unit: only whole numbers have one agreed spelling to sign. */
214
+ /** Fields of a place that are changed by their own operations, not by `field.set`. */
215
+ const PLACE_RESERVED = new Set(["place", "kind", "parent", "grid", "standing", "retired", "adrift"]);
216
+
217
+ /** The most positions a box may have along one side, and the farthest a place may stand or reach, in millimetres. */
218
+ const MOST_SIDE = 100;
219
+ const FARTHEST = 1_000_000;
220
+
221
+ const isGrid = (value: unknown): value is { rows: number; columns: number } => {
222
+ const grid = value as { rows?: unknown; columns?: unknown } | null;
223
+ return typeof grid === "object" && grid !== null && [grid.rows, grid.columns].every((side) => Number.isSafeInteger(side) && (side as number) >= 1 && (side as number) <= MOST_SIDE);
224
+ };
225
+
226
+ const isSlot = (value: unknown): value is { row: number; column: number } => {
227
+ const slot = value as { row?: unknown; column?: unknown } | null;
228
+ return typeof slot === "object" && slot !== null && [slot.row, slot.column].every((at) => Number.isSafeInteger(at) && (at as number) >= 0);
229
+ };
230
+
231
+ const isStanding = (value: unknown): value is StandingMm => {
232
+ const standing = value as Partial<StandingMm> | null;
233
+ const three = (list: unknown, least: number): boolean =>
234
+ Array.isArray(list) && list.length === 3 && list.every((one) => Number.isSafeInteger(one) && one >= least && one <= FARTHEST);
235
+ return typeof standing === "object" && standing !== null && three(standing.at, -FARTHEST) && three(standing.size, 1)
236
+ && [0, 90, 180, 270].includes(standing.turn as number);
237
+ };
238
+
171
239
  const amountOf = (value: unknown): value is number => typeof value === "number" && Number.isSafeInteger(value);
172
240
  const role = (value: unknown): value is Role => value === "member" || value === "keeper";
173
241
 
@@ -194,7 +262,7 @@ const hadSeen = (later: Node, earlier: Node): boolean => (later.vc.get(earlier.a
194
262
  * ```
195
263
  */
196
264
  export async function mergeLog(entries: readonly unknown[], options: { lab?: string } = {}): Promise<LabState> {
197
- const state: LabState = { lab: options.lab ?? null, name: null, roster: [], parts: [], items: [], entries: [],
265
+ const state: LabState = { lab: options.lab ?? null, name: null, roster: [], parts: [], items: [], places: [], crowded: [], entries: [],
198
266
  waiting: [], discarded: [], overlaps: [], heads: {}, knows: {} };
199
267
 
200
268
  // Read and check every entry on its own, before any of them is believed about the others.
@@ -441,6 +509,10 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
441
509
  if (wins) set.set(field, { node, value });
442
510
  };
443
511
 
512
+ const places = new Map<string, { place: PlaceState; set: Map<string, Register> }>();
513
+ /** Where each item was last put. */
514
+ const put = new Map<string, Map<string, Register>>();
515
+
444
516
  for (const one of placed) {
445
517
  if (one.status !== "applied") continue;
446
518
  const node = chained.get(one.id)!;
@@ -485,7 +557,7 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
485
557
  refuse("The part it is an item of is not one the lab has.");
486
558
  } else {
487
559
  items.set(op["item"], {
488
- item: { item: op["item"], part: op["part"], unit: op["unit"], amount: 0, retired: false, needsRecount: false, fields: {}, added: node.id },
560
+ item: { item: op["item"], part: op["part"], unit: op["unit"], amount: 0, retired: false, needsRecount: false, where: null, fields: {}, added: node.id },
489
561
  set: new Map(), base: { node, amount: op["amount"], recount: false }, adjusted: [], overlapped: false,
490
562
  });
491
563
  }
@@ -511,12 +583,64 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
511
583
  } else {
512
584
  held.item.retired = true;
513
585
  }
586
+ } else if (op.type === "place.add") {
587
+ const grid = op["grid"];
588
+ if (!(typeof op["place"] === "string" && ITEM_ID.test(op["place"])) || !text(op["kind"], 40) || !text(op["name"], 200)
589
+ || !(grid === null || isGrid(grid))) {
590
+ refuse("It does not carry a place ID, a kind, a name, and positions that are whole numbers.");
591
+ } else if (places.has(op["place"])) {
592
+ refuse("A place with that ID already exists.");
593
+ } else if (!(op["parent"] === null || (typeof op["parent"] === "string" && places.has(op["parent"])))) {
594
+ refuse("The place it is in is not one the lab has.");
595
+ } else {
596
+ places.set(op["place"], {
597
+ place: { place: op["place"], kind: op["kind"], name: op["name"], parent: null, grid: grid === null ? null : { rows: grid.rows, columns: grid.columns },
598
+ standing: null, retired: false, adrift: false, fields: {}, added: node.id },
599
+ set: new Map<string, Register>([["name", { node, value: op["name"] }], ["parent", { node, value: op["parent"] }]]),
600
+ });
601
+ }
602
+ } else if (op.type === "place.move" || op.type === "place.stand" || op.type === "place.retire") {
603
+ const held = typeof op["place"] === "string" ? places.get(op["place"]) : undefined;
604
+ if (held === undefined) { refuse("The place it changes is not one the lab has."); continue; }
605
+ if (op.type === "place.move") {
606
+ const parent = op["parent"];
607
+ if (!(parent === null || (typeof parent === "string" && places.has(parent)))) { refuse("The place it moves to is not one the lab has."); continue; }
608
+ if (parent === op["place"]) { refuse("A place cannot be inside itself."); continue; }
609
+ write(held.set, "parent", node, parent, `where ${held.place.name} is`);
610
+ } else if (op.type === "place.stand") {
611
+ const standing = op["standing"];
612
+ if (!(standing === null || isStanding(standing))) { refuse("It does not say where the place stands and how big it is, in whole millimetres."); continue; }
613
+ write(held.set, "standing", node, standing, `where ${held.place.name} stands`);
614
+ } else {
615
+ held.place.retired = true;
616
+ }
617
+ } else if (op.type === "item.put") {
618
+ const item = typeof op["item"] === "string" ? items.get(op["item"]) : undefined;
619
+ if (item === undefined) { refuse("The item it puts somewhere is not one the lab has."); continue; }
620
+ const place = op["place"];
621
+ const slot = op["slot"];
622
+ if (place === null) {
623
+ if (slot !== null) { refuse("An item that is nowhere is at no position."); continue; }
624
+ } else {
625
+ const into = typeof place === "string" ? places.get(place) : undefined;
626
+ if (into === undefined) { refuse("The place it puts the item in is not one the lab has."); continue; }
627
+ if (slot !== null && !(isSlot(slot) && into.place.grid !== null && slot.row < into.place.grid.rows && slot.column < into.place.grid.columns)) {
628
+ refuse("The position it names is not one that place has.");
629
+ continue;
630
+ }
631
+ }
632
+ if (!put.has(item.item.item)) put.set(item.item.item, new Map());
633
+ write(put.get(item.item.item)!, "where", node, place === null ? null : { place, slot: slot === null ? null : { row: (slot as { row: number }).row, column: (slot as { column: number }).column } },
634
+ `where item ${item.item.item.slice(0, 12)} is`);
514
635
  } else if (op.type === "field.set") {
515
636
  const field = op["field"];
516
637
  const value = op["value"];
517
638
  const target = op["on"] === "part" && typeof op["id"] === "string" ? parts.get(op["id"])
518
- : op["on"] === "item" && typeof op["id"] === "string" ? items.get(op["id"]) : undefined;
519
- if (target === undefined) { refuse("It does not name a part or an item the lab has."); continue; }
639
+ : op["on"] === "item" && typeof op["id"] === "string" ? items.get(op["id"])
640
+ : op["on"] === "place" && typeof op["id"] === "string" ? places.get(op["id"]) : undefined;
641
+ if (target === undefined) { refuse("It does not name a part, an item or a place the lab has."); continue; }
642
+ if (op["on"] === "place" && typeof field === "string" && PLACE_RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
643
+ if (field === "name" && op["on"] === "place" && !text(value, 200)) { refuse("A place's name has to be some text."); continue; }
520
644
  if (typeof field !== "string" || !FIELD.test(field) || RESERVED.has(field)) { refuse("It does not name a field that can be set."); continue; }
521
645
  if (!(value === null || amountOf(value) || (typeof value === "string" && value.length <= 2_000))) { refuse("Its value is not text, a whole number or nothing."); continue; }
522
646
  if (field === "name" && op["on"] === "part" && !text(value, 200)) { refuse("A part's name has to be some text."); continue; }
@@ -541,8 +665,38 @@ function fold(state: LabState, placed: Placed[], chained: Map<string, Node>): vo
541
665
  item.amount = base.amount + since;
542
666
  item.needsRecount = item.amount < 0 || overlapped;
543
667
  for (const [field, held] of set) item.fields[field] = held.value as string | number | null;
668
+ item.where = (put.get(item.item)?.get("where")?.value ?? null) as ItemState["where"];
544
669
  state.items.push(item);
545
670
  }
671
+ for (const { place, set } of places.values()) {
672
+ for (const [field, held] of set) {
673
+ if (field === "name") place.name = held.value as string;
674
+ else if (field === "parent") place.parent = held.value as string | null;
675
+ else if (field === "standing") place.standing = held.value as StandingMm | null;
676
+ else place.fields[field] = held.value as string | number | null;
677
+ }
678
+ state.places.push(place);
679
+ }
680
+ // Two people apart can each move a place into the other. Neither move is wrong alone, so neither
681
+ // is refused: every place caught in the loop is shown as being in nothing, and says so.
682
+ for (const { place } of places.values()) {
683
+ const passed = new Set<string>();
684
+ for (let up = place.parent; up !== null && !passed.has(up); up = places.get(up)!.place.parent) {
685
+ if (up === place.place) { place.adrift = true; break; }
686
+ passed.add(up);
687
+ }
688
+ }
689
+ for (const { place } of places.values()) if (place.adrift) place.parent = null;
690
+ // Two tubes cannot stand in one position, but two people apart can each say theirs does.
691
+ const stood = new Map<string, Crowded>();
692
+ for (const item of state.items) {
693
+ if (item.retired || item.where === null || item.where.slot === null) continue;
694
+ const key = `${item.where.place} ${item.where.slot.row} ${item.where.slot.column}`;
695
+ const held = stood.get(key) ?? { place: item.where.place, slot: item.where.slot, items: [] };
696
+ held.items.push(item.item);
697
+ stood.set(key, held);
698
+ }
699
+ state.crowded = [...stood.values()].filter((one) => one.items.length > 1);
546
700
  }
547
701
 
548
702
  /**
package/src/model.ts CHANGED
@@ -10,8 +10,8 @@
10
10
  * circle, wherever you cut it open. Two labs holding the same molecule publish the same checksum
11
11
  * without having agreed on anything.
12
12
  *
13
- * **The address is the identity.** An inventory signed by a key and served from `endylab.example` ties
14
- * that key to `endylab.example` and to nothing else. There is no registry, no authority and nobody to
13
+ * **The address is the identity.** An inventory signed by a key and served from `wrenlab.example` ties
14
+ * that key to `wrenlab.example` and to nothing else. There is no registry, no authority and nobody to
15
15
  * ask: you get a lab's key the way you got its web address, and what you can catch afterwards is the
16
16
  * key *changing*.
17
17
  *
@@ -27,7 +27,7 @@ export interface LabModel {
27
27
  /** What the lab calls itself. A label, not a verified name. */
28
28
  name: string;
29
29
  /**
30
- * The web address the inventory is published at, as an origin like `https://endylab.example`.
30
+ * The web address the inventory is published at, as an origin like `https://wrenlab.example`.
31
31
  *
32
32
  * Fetching checks this against where the document actually came from. A document claiming an origin
33
33
  * it was not served from is refused, because that claim is the whole of the binding.
package/src/origin.ts CHANGED
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * A lab publishes at a fixed path on its own web address, the way a site publishes other well-known
5
5
  * files. Then the address does the work an authority would otherwise have to do: if you already know
6
- * that `endylab.example` is the Endy lab — because you read it in a paper, or a colleague told you, or
7
- * it is written on their door — then an inventory served from `https://endylab.example` and signed by
6
+ * that `wrenlab.example` is the Wren lab — because you read it in a paper, or a colleague told you, or
7
+ * it is written on their door — then an inventory served from `https://wrenlab.example` and signed by
8
8
  * a key ties that key to that lab, as firmly as you trust the address and the connection.
9
9
  *
10
10
  * That is the whole of it, and it is worth being exact about the edges:
@@ -27,11 +27,11 @@ const loopback = (host: string): boolean =>
27
27
  /**
28
28
  * Read what somebody typed as the address of a lab.
29
29
  *
30
- * Accepts `endylab.example`, `https://endylab.example`, or an address with a path on it, and gives
30
+ * Accepts `wrenlab.example`, `https://wrenlab.example`, or an address with a path on it, and gives
31
31
  * back the origin alone, which is all that identifies the lab.
32
32
  *
33
33
  * @param address - What they typed.
34
- * @returns The origin, like `https://endylab.example`.
34
+ * @returns The origin, like `https://wrenlab.example`.
35
35
  * @throws If it is not an address, or is one whose connection would prove nothing.
36
36
  */
37
37
  export function labOrigin(address: string): string {