@kurvball/rehearse 1.2.1 → 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.
@@ -0,0 +1,140 @@
1
+ import { chainOf } from '../worlds/index.js';
2
+ export function slotsFor(n) {
3
+ let free = Math.max(1, Math.floor(n));
4
+ const waiting = [];
5
+ return {
6
+ async take() {
7
+ if (free > 0)
8
+ free -= 1;
9
+ else
10
+ await new Promise((resolve) => waiting.push(resolve));
11
+ let held = true;
12
+ return () => {
13
+ if (!held)
14
+ return;
15
+ held = false;
16
+ const next = waiting.shift();
17
+ if (next)
18
+ next();
19
+ else
20
+ free += 1;
21
+ };
22
+ },
23
+ };
24
+ }
25
+ const NO_SERVICE = ' no service';
26
+ const NAMES_A_SERVICE = /\{\{(?:url|[A-Za-z0-9_-]+_(?:url|port))\}\}/;
27
+ const worldBuildsSomething = (m, world) => world !== null && chainOf(m, world).some((w) => w.before !== null || w.add !== null);
28
+ function signInLeansOnServices(m) {
29
+ const commands = [
30
+ m.signIn?.command ?? '',
31
+ ...Object.values(m.people).map((p) => (p.token && 'command' in p.token ? p.token.command : '')),
32
+ ];
33
+ return commands.some((c) => NAMES_A_SERVICE.test(c));
34
+ }
35
+ function servicesNamingEachOther(m) {
36
+ const pairs = [];
37
+ for (const s of Object.values(m.services)) {
38
+ const said = JSON.stringify([
39
+ s,
40
+ Object.values(m.standins).filter((x) => x.services?.includes(s.name) ?? true),
41
+ ]);
42
+ for (const other of Object.keys(m.services))
43
+ if (other !== s.name && (said.includes(`{{${other}_url}}`) || said.includes(`{{${other}_port}}`)))
44
+ pairs.push([s.name, other]);
45
+ }
46
+ return pairs;
47
+ }
48
+ export function unitsOf(m, w) {
49
+ const holdsOf = (needed) => new Set(needed.size ? needed : [NO_SERVICE]);
50
+ const runnersFor = (members) => new Map([...w.runners].filter(([kind]) => members.some((s) => s.kind === kind)));
51
+ const builds = worldBuildsSomething(m, w.world);
52
+ if (builds || signInLeansOnServices(m))
53
+ return [
54
+ {
55
+ at: w.at,
56
+ world: w.world,
57
+ members: [...w.group],
58
+ runners: new Map(w.runners),
59
+ needed: new Set(w.needed),
60
+ vouched: w.vouched,
61
+ holds: holdsOf(w.needed),
62
+ holdsTheWholeCheckout: builds,
63
+ },
64
+ ];
65
+ const leader = new Map();
66
+ const find = (name) => {
67
+ const up = leader.get(name) ?? name;
68
+ if (up === name)
69
+ return name;
70
+ const top = find(up);
71
+ leader.set(name, top);
72
+ return top;
73
+ };
74
+ const join = (a, b) => {
75
+ const [x, y] = [find(a), find(b)];
76
+ if (x !== y)
77
+ leader.set(y, x);
78
+ };
79
+ for (const [a, b] of servicesNamingEachOther(m))
80
+ join(a, b);
81
+ for (const s of w.group) {
82
+ const [first, ...rest] = w.servicesOf(s);
83
+ for (const name of rest)
84
+ join(first, name);
85
+ }
86
+ const byLeader = new Map();
87
+ for (const s of w.group) {
88
+ const first = w.servicesOf(s)[0];
89
+ const key = first === undefined ? NO_SERVICE : find(first);
90
+ byLeader.set(key, [...(byLeader.get(key) ?? []), s]);
91
+ }
92
+ return [...byLeader.values()].map((members) => {
93
+ const needed = new Set(members.flatMap((s) => w.servicesOf(s)));
94
+ return {
95
+ at: w.at,
96
+ world: w.world,
97
+ members,
98
+ runners: runnersFor(members),
99
+ needed,
100
+ vouched: w.vouched,
101
+ holds: holdsOf(needed),
102
+ holdsTheWholeCheckout: false,
103
+ };
104
+ });
105
+ }
106
+ const clash = (a, b) => a.holdsTheWholeCheckout || b.holdsTheWholeCheckout || [...a.holds].some((x) => b.holds.has(x));
107
+ export async function inTurn(units, slots, work, skip) {
108
+ const results = units.map(() => null);
109
+ const waiting = units.map((_, i) => i);
110
+ const busy = new Map();
111
+ const failures = [];
112
+ const startable = () => waiting.find((i, k) => ![...busy.keys()].some((j) => clash(units[i], units[j])) &&
113
+ !waiting.slice(0, k).some((j) => clash(units[i], units[j])));
114
+ for (;;) {
115
+ for (const i of waiting.filter((x) => skip(units[x]) || failures.length > 0))
116
+ waiting.splice(waiting.indexOf(i), 1);
117
+ const next = startable();
118
+ if (next === undefined) {
119
+ if (busy.size === 0)
120
+ break;
121
+ await Promise.race(busy.values());
122
+ continue;
123
+ }
124
+ const free = await slots.take();
125
+ if (skip(units[next]) || failures.length > 0) {
126
+ free();
127
+ continue;
128
+ }
129
+ waiting.splice(waiting.indexOf(next), 1);
130
+ busy.set(next, work(units[next])
131
+ .then((r) => void (results[next] = r), (error) => void failures.push(error))
132
+ .finally(() => {
133
+ free();
134
+ busy.delete(next);
135
+ }));
136
+ }
137
+ if (failures.length > 0)
138
+ throw failures[0];
139
+ return results;
140
+ }
@@ -4,6 +4,7 @@ import { type Manifest } from '../manifest/index.ts';
4
4
  import { applyWorld } from '../worlds/index.ts';
5
5
  export { applyWorld };
6
6
  import type { CheckoutInfo, Observation } from './observation.ts';
7
+ import { type Slots } from './parallel.ts';
7
8
  export interface SessionOptions {
8
9
  world: string | null;
9
10
  as: string | null;
@@ -12,6 +13,7 @@ export interface SessionOptions {
12
13
  progress: (line: string) => void;
13
14
  ref: string | null;
14
15
  groups?: ReadonlyMap<string, string>;
16
+ parallel?: number;
15
17
  }
16
18
  export interface NotRun {
17
19
  scenario: Scenario;
@@ -30,3 +32,4 @@ export interface SideRun {
30
32
  export declare function standinUses(m: Manifest, services: readonly string[], real: ReadonlySet<string>): StandinUse[];
31
33
  export declare function placedIn<T>(value: T, folders: readonly [string, string][]): T;
32
34
  export declare function runSide(m: Manifest, checkout: string, scenarios: readonly Scenario[], o: SessionOptions): Promise<SideRun>;
35
+ export declare function runSideSharing(m: Manifest, checkout: string, scenarios: readonly Scenario[], o: SessionOptions, slots: Slots): Promise<SideRun>;