@kurvball/rehearse 0.0.0-stage → 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.
Files changed (270) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +107 -2
  3. package/dist/affected/affected.d.ts +22 -0
  4. package/dist/affected/affected.js +162 -0
  5. package/dist/affected/index.d.ts +1 -0
  6. package/dist/affected/index.js +1 -0
  7. package/dist/args.d.ts +12 -0
  8. package/dist/args.js +42 -0
  9. package/dist/cli.d.ts +4 -0
  10. package/dist/cli.js +100 -0
  11. package/dist/commands/check.d.ts +2 -0
  12. package/dist/commands/check.js +42 -0
  13. package/dist/commands/clean.d.ts +2 -0
  14. package/dist/commands/clean.js +37 -0
  15. package/dist/commands/command.d.ts +30 -0
  16. package/dist/commands/command.js +38 -0
  17. package/dist/commands/compare.d.ts +4 -0
  18. package/dist/commands/compare.js +89 -0
  19. package/dist/commands/drive.d.ts +2 -0
  20. package/dist/commands/drive.js +244 -0
  21. package/dist/commands/index.d.ts +3 -0
  22. package/dist/commands/index.js +32 -0
  23. package/dist/commands/init.d.ts +2 -0
  24. package/dist/commands/init.js +19 -0
  25. package/dist/commands/list.d.ts +2 -0
  26. package/dist/commands/list.js +69 -0
  27. package/dist/commands/open.d.ts +2 -0
  28. package/dist/commands/open.js +60 -0
  29. package/dist/commands/people.d.ts +2 -0
  30. package/dist/commands/people.js +49 -0
  31. package/dist/commands/run.d.ts +4 -0
  32. package/dist/commands/run.js +84 -0
  33. package/dist/commands/services.d.ts +28 -0
  34. package/dist/commands/services.js +51 -0
  35. package/dist/commands/stage.d.ts +7 -0
  36. package/dist/commands/stage.js +53 -0
  37. package/dist/commands/up.d.ts +3 -0
  38. package/dist/commands/up.js +77 -0
  39. package/dist/commands/validate.d.ts +2 -0
  40. package/dist/commands/validate.js +35 -0
  41. package/dist/commands/worlds.d.ts +2 -0
  42. package/dist/commands/worlds.js +15 -0
  43. package/dist/compare/announce.d.ts +6 -0
  44. package/dist/compare/announce.js +27 -0
  45. package/dist/compare/compare.d.ts +83 -0
  46. package/dist/compare/compare.js +335 -0
  47. package/dist/compare/diff.d.ts +15 -0
  48. package/dist/compare/diff.js +105 -0
  49. package/dist/compare/index.d.ts +5 -0
  50. package/dist/compare/index.js +5 -0
  51. package/dist/compare/setaside.d.ts +29 -0
  52. package/dist/compare/setaside.js +121 -0
  53. package/dist/compare/verdict.d.ts +21 -0
  54. package/dist/compare/verdict.js +30 -0
  55. package/dist/drive/aria.d.ts +8 -0
  56. package/dist/drive/aria.js +12 -0
  57. package/dist/drive/browser.d.ts +34 -0
  58. package/dist/drive/browser.js +216 -0
  59. package/dist/drive/daemon.d.ts +1 -0
  60. package/dist/drive/daemon.js +171 -0
  61. package/dist/drive/grammar.d.ts +93 -0
  62. package/dist/drive/grammar.js +303 -0
  63. package/dist/drive/index.d.ts +4 -0
  64. package/dist/drive/index.js +4 -0
  65. package/dist/drive/names.d.ts +24 -0
  66. package/dist/drive/names.js +97 -0
  67. package/dist/drive/state.d.ts +13 -0
  68. package/dist/drive/state.js +28 -0
  69. package/dist/errors.d.ts +26 -0
  70. package/dist/errors.js +32 -0
  71. package/dist/exec.d.ts +16 -0
  72. package/dist/exec.js +43 -0
  73. package/dist/git/checkout.d.ts +11 -0
  74. package/dist/git/checkout.js +90 -0
  75. package/dist/git/git.d.ts +7 -0
  76. package/dist/git/git.js +27 -0
  77. package/dist/git/index.d.ts +2 -0
  78. package/dist/git/index.js +2 -0
  79. package/dist/git/seed.d.ts +3 -0
  80. package/dist/git/seed.js +115 -0
  81. package/dist/index.d.ts +18 -0
  82. package/dist/index.js +15 -0
  83. package/dist/kinds/all.d.ts +4 -0
  84. package/dist/kinds/all.js +4 -0
  85. package/dist/kinds/call/check.d.ts +29 -0
  86. package/dist/kinds/call/check.js +187 -0
  87. package/dist/kinds/call/definition.d.ts +16 -0
  88. package/dist/kinds/call/definition.js +33 -0
  89. package/dist/kinds/call/hurl.d.ts +65 -0
  90. package/dist/kinds/call/hurl.js +333 -0
  91. package/dist/kinds/call/kind.d.ts +8 -0
  92. package/dist/kinds/call/kind.js +122 -0
  93. package/dist/kinds/call/send.d.ts +32 -0
  94. package/dist/kinds/call/send.js +152 -0
  95. package/dist/kinds/call/words.d.ts +22 -0
  96. package/dist/kinds/call/words.js +24 -0
  97. package/dist/kinds/file/kind.d.ts +44 -0
  98. package/dist/kinds/file/kind.js +331 -0
  99. package/dist/kinds/file/layouts.d.ts +14 -0
  100. package/dist/kinds/file/layouts.js +103 -0
  101. package/dist/kinds/flow/kind.d.ts +21 -0
  102. package/dist/kinds/flow/kind.js +412 -0
  103. package/dist/kinds/flow/literal.d.ts +8 -0
  104. package/dist/kinds/flow/literal.js +148 -0
  105. package/dist/kinds/index.d.ts +10 -0
  106. package/dist/kinds/index.js +8 -0
  107. package/dist/kinds/kind.d.ts +63 -0
  108. package/dist/kinds/kind.js +21 -0
  109. package/dist/kinds/piece/canvas.d.ts +18 -0
  110. package/dist/kinds/piece/canvas.js +165 -0
  111. package/dist/kinds/piece/conventions.d.ts +12 -0
  112. package/dist/kinds/piece/conventions.js +101 -0
  113. package/dist/kinds/piece/csf.d.ts +10 -0
  114. package/dist/kinds/piece/csf.js +304 -0
  115. package/dist/kinds/piece/kind.d.ts +12 -0
  116. package/dist/kinds/piece/kind.js +297 -0
  117. package/dist/kinds/piece/server.d.ts +23 -0
  118. package/dist/kinds/piece/server.js +236 -0
  119. package/dist/manifest/fill.d.ts +4 -0
  120. package/dist/manifest/fill.js +7 -0
  121. package/dist/manifest/index.d.ts +5 -0
  122. package/dist/manifest/index.js +5 -0
  123. package/dist/manifest/init.d.ts +1 -0
  124. package/dist/manifest/init.js +47 -0
  125. package/dist/manifest/load.d.ts +7 -0
  126. package/dist/manifest/load.js +245 -0
  127. package/dist/manifest/types.d.ts +255 -0
  128. package/dist/manifest/types.js +7 -0
  129. package/dist/manifest/yaml.d.ts +5 -0
  130. package/dist/manifest/yaml.js +47 -0
  131. package/dist/people/index.d.ts +1 -0
  132. package/dist/people/index.js +1 -0
  133. package/dist/people/people.d.ts +39 -0
  134. package/dist/people/people.js +139 -0
  135. package/dist/run/cache.d.ts +20 -0
  136. package/dist/run/cache.js +98 -0
  137. package/dist/run/index.d.ts +5 -0
  138. package/dist/run/index.js +4 -0
  139. package/dist/run/observation.d.ts +27 -0
  140. package/dist/run/observation.js +1 -0
  141. package/dist/run/parallel.d.ts +28 -0
  142. package/dist/run/parallel.js +140 -0
  143. package/dist/run/session.d.ts +35 -0
  144. package/dist/run/session.js +399 -0
  145. package/dist/run/vars.d.ts +3 -0
  146. package/dist/run/vars.js +13 -0
  147. package/dist/scenarios/area.d.ts +6 -0
  148. package/dist/scenarios/area.js +37 -0
  149. package/dist/scenarios/files.d.ts +9 -0
  150. package/dist/scenarios/files.js +26 -0
  151. package/dist/scenarios/glob.d.ts +6 -0
  152. package/dist/scenarios/glob.js +106 -0
  153. package/dist/scenarios/index.d.ts +4 -0
  154. package/dist/scenarios/index.js +4 -0
  155. package/dist/scenarios/read.d.ts +18 -0
  156. package/dist/scenarios/read.js +54 -0
  157. package/dist/scenarios/scenario.d.ts +34 -0
  158. package/dist/scenarios/scenario.js +10 -0
  159. package/dist/schema.d.ts +14 -0
  160. package/dist/schema.js +122 -0
  161. package/dist/services/index.d.ts +2 -0
  162. package/dist/services/index.js +2 -0
  163. package/dist/services/registry.d.ts +23 -0
  164. package/dist/services/registry.js +49 -0
  165. package/dist/services/service.d.ts +23 -0
  166. package/dist/services/service.js +313 -0
  167. package/dist/stage/api.d.ts +30 -0
  168. package/dist/stage/api.js +206 -0
  169. package/dist/stage/client/client/api.js +11 -0
  170. package/dist/stage/client/client/index.js +1 -0
  171. package/dist/stage/client/features/canvas/call.js +65 -0
  172. package/dist/stage/client/features/canvas/context.js +1 -0
  173. package/dist/stage/client/features/canvas/file.js +36 -0
  174. package/dist/stage/client/features/canvas/flow.js +59 -0
  175. package/dist/stage/client/features/canvas/index.js +1 -0
  176. package/dist/stage/client/features/canvas/observed.js +8 -0
  177. package/dist/stage/client/features/canvas/parts.js +24 -0
  178. package/dist/stage/client/features/canvas/piece.js +31 -0
  179. package/dist/stage/client/features/canvas/reach.js +6 -0
  180. package/dist/stage/client/features/canvas/views.js +16 -0
  181. package/dist/stage/client/features/list/index.js +1 -0
  182. package/dist/stage/client/features/list/list.js +53 -0
  183. package/dist/stage/client/features/list/reach.js +10 -0
  184. package/dist/stage/client/features/scenario/heading.js +14 -0
  185. package/dist/stage/client/features/scenario/index.js +1 -0
  186. package/dist/stage/client/features/scenario/reach.js +9 -0
  187. package/dist/stage/client/features/scenario/scenario.js +145 -0
  188. package/dist/stage/client/features/try/index.js +1 -0
  189. package/dist/stage/client/features/try/reach.js +12 -0
  190. package/dist/stage/client/features/try/try.js +65 -0
  191. package/dist/stage/client/index.html +16 -0
  192. package/dist/stage/client/main.js +24 -0
  193. package/dist/stage/client/primitives/busy.js +6 -0
  194. package/dist/stage/client/primitives/focus.js +21 -0
  195. package/dist/stage/client/primitives/h.js +25 -0
  196. package/dist/stage/client/primitives/index.js +6 -0
  197. package/dist/stage/client/primitives/keyed.js +16 -0
  198. package/dist/stage/client/primitives/say.js +6 -0
  199. package/dist/stage/client/primitives/state.js +21 -0
  200. package/dist/stage/client/primitives/types.js +1 -0
  201. package/dist/stage/client/primitives/verdicts.js +8 -0
  202. package/dist/stage/client/stage.css +652 -0
  203. package/dist/stage/compared.d.ts +20 -0
  204. package/dist/stage/compared.js +58 -0
  205. package/dist/stage/files.d.ts +4 -0
  206. package/dist/stage/files.js +59 -0
  207. package/dist/stage/guard.d.ts +3 -0
  208. package/dist/stage/guard.js +20 -0
  209. package/dist/stage/http.d.ts +12 -0
  210. package/dist/stage/http.js +34 -0
  211. package/dist/stage/index.d.ts +1 -0
  212. package/dist/stage/index.js +1 -0
  213. package/dist/stage/sending.d.ts +18 -0
  214. package/dist/stage/sending.js +66 -0
  215. package/dist/stage/server.d.ts +33 -0
  216. package/dist/stage/server.js +253 -0
  217. package/dist/version.d.ts +2 -0
  218. package/dist/version.js +5 -0
  219. package/dist/worlds/define.d.ts +16 -0
  220. package/dist/worlds/define.js +45 -0
  221. package/dist/worlds/index.d.ts +2 -0
  222. package/dist/worlds/index.js +2 -0
  223. package/dist/worlds/worlds.d.ts +15 -0
  224. package/dist/worlds/worlds.js +46 -0
  225. package/llms.txt +46 -0
  226. package/package.json +84 -3
  227. package/schema/v1/announce.schema.json +28 -0
  228. package/schema/v1/check.schema.json +67 -0
  229. package/schema/v1/clean.schema.json +19 -0
  230. package/schema/v1/common.schema.json +210 -0
  231. package/schema/v1/comparison.schema.json +298 -0
  232. package/schema/v1/dependents.schema.json +30 -0
  233. package/schema/v1/drive.schema.json +112 -0
  234. package/schema/v1/error.schema.json +26 -0
  235. package/schema/v1/file-scenario.schema.json +175 -0
  236. package/schema/v1/flow.schema.json +216 -0
  237. package/schema/v1/init.schema.json +20 -0
  238. package/schema/v1/list.schema.json +70 -0
  239. package/schema/v1/manifest.schema.json +434 -0
  240. package/schema/v1/observation.schema.json +388 -0
  241. package/schema/v1/open.schema.json +14 -0
  242. package/schema/v1/people.schema.json +88 -0
  243. package/schema/v1/samples/announce.yaml +5 -0
  244. package/schema/v1/samples/check.json +13 -0
  245. package/schema/v1/samples/clean.json +1 -0
  246. package/schema/v1/samples/comparison.json +83 -0
  247. package/schema/v1/samples/dependents.json +7 -0
  248. package/schema/v1/samples/drive.json +10 -0
  249. package/schema/v1/samples/error.json +10 -0
  250. package/schema/v1/samples/file-scenario.yaml +13 -0
  251. package/schema/v1/samples/flow.yaml +13 -0
  252. package/schema/v1/samples/init.json +1 -0
  253. package/schema/v1/samples/list.json +24 -0
  254. package/schema/v1/samples/manifest.yaml +55 -0
  255. package/schema/v1/samples/observation.json +49 -0
  256. package/schema/v1/samples/open.json +6 -0
  257. package/schema/v1/samples/people.json +11 -0
  258. package/schema/v1/samples/services.json +27 -0
  259. package/schema/v1/samples/stage.json +6 -0
  260. package/schema/v1/samples/try.json +13 -0
  261. package/schema/v1/samples/up.json +16 -0
  262. package/schema/v1/samples/validate.json +7 -0
  263. package/schema/v1/samples/worlds.json +21 -0
  264. package/schema/v1/services.schema.json +72 -0
  265. package/schema/v1/stage.schema.json +14 -0
  266. package/schema/v1/try.schema.json +56 -0
  267. package/schema/v1/up.schema.json +55 -0
  268. package/schema/v1/validate.schema.json +40 -0
  269. package/schema/v1/worlds.schema.json +33 -0
  270. package/skill/rehearse/SKILL.md +148 -0
@@ -0,0 +1,139 @@
1
+ import { RehearseError } from '../errors.js';
2
+ import { sh, tail } from '../exec.js';
3
+ export const NOBODY = 'nobody';
4
+ export const NO_ONE = 'a token for no one';
5
+ const THE_SERVICES_ADDRESS = 'api';
6
+ const WHOEVER_RUNS_REHEARSE = 'you';
7
+ export const NAMES_A_PERSON_CANT_TAKE = new Set([
8
+ WHOEVER_RUNS_REHEARSE,
9
+ NOBODY,
10
+ NO_ONE,
11
+ THE_SERVICES_ADDRESS,
12
+ ]);
13
+ const NAME = /^[A-Za-z_][A-Za-z0-9_-]*$/;
14
+ export const isVariableName = (v) => NAME.test(v);
15
+ const isRecord = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
16
+ const scalar = (v) => typeof v === 'string' ? v : typeof v === 'number' || typeof v === 'boolean' ? String(v) : null;
17
+ export function signedInFrom(stdout) {
18
+ let raw;
19
+ try {
20
+ raw = JSON.parse(stdout.trim());
21
+ }
22
+ catch {
23
+ return null;
24
+ }
25
+ if (!isRecord(raw) || !isRecord(raw.people))
26
+ return null;
27
+ const out = { tokens: {}, fields: {}, variables: {} };
28
+ for (const [name, said] of Object.entries(raw.people)) {
29
+ if (!isVariableName(name) || NAMES_A_PERSON_CANT_TAKE.has(name))
30
+ continue;
31
+ const token = isRecord(said) ? scalar(said.token) : scalar(said);
32
+ if (!token)
33
+ continue;
34
+ out.tokens[name] = token;
35
+ out.fields[name] = {};
36
+ if (isRecord(said))
37
+ for (const [key, value] of Object.entries(said)) {
38
+ const v = scalar(value);
39
+ if (key === 'token' || v === null || !isVariableName(key))
40
+ continue;
41
+ out.fields[name][key] = v;
42
+ out.variables[`${name}_${key}`] = v;
43
+ }
44
+ }
45
+ if (isRecord(raw.variables))
46
+ for (const [key, value] of Object.entries(raw.variables)) {
47
+ const v = scalar(value);
48
+ if (v !== null && isVariableName(key) && !NAMES_A_PERSON_CANT_TAKE.has(key))
49
+ out.variables[key] = v;
50
+ }
51
+ return out;
52
+ }
53
+ export async function signIn(m, place) {
54
+ const out = { tokens: {}, fields: {}, variables: {} };
55
+ if (m.signIn) {
56
+ const command = place.fill(m.signIn.command);
57
+ const ran = await sh(command, {
58
+ cwd: m.signIn.cwd ? `${place.cwd}/${m.signIn.cwd}` : place.cwd,
59
+ env: place.env,
60
+ timeoutMs: 30_000,
61
+ });
62
+ if (ran.code !== 0) {
63
+ const tokensOnStdout = signedInFrom(ran.stdout)?.tokens ?? {};
64
+ const said = scrub(tail(ran.stderr), tokensOnStdout);
65
+ throw new RehearseError('sign_in', `The sign-in \`${m.signIn.command}\` failed (exit ${ran.code})${said ? `: ${said}` : '.'}`);
66
+ }
67
+ const read = signedInFrom(ran.stdout);
68
+ if (!read)
69
+ throw new RehearseError('sign_in', `The sign-in \`${m.signIn.command}\` didn't print its people as JSON: {"people": {"name": "token"}}.`);
70
+ Object.assign(out.tokens, read.tokens);
71
+ Object.assign(out.fields, read.fields);
72
+ Object.assign(out.variables, read.variables);
73
+ }
74
+ for (const p of Object.values(m.people)) {
75
+ if (out.tokens[p.name] || !p.token)
76
+ continue;
77
+ if ('env' in p.token) {
78
+ const v = process.env[p.token.env];
79
+ if (v)
80
+ out.tokens[p.name] = v;
81
+ continue;
82
+ }
83
+ const ran = await sh(place.fill(p.token.command), { cwd: place.cwd, env: place.env, timeoutMs: 30_000 });
84
+ if (ran.code !== 0)
85
+ throw new RehearseError('sign_in', `${p.name}'s token command failed${ran.stderr ? `: ${tail(ran.stderr)}` : '.'}`);
86
+ const token = ran.stdout.trim();
87
+ if (token)
88
+ out.tokens[p.name] = token;
89
+ }
90
+ return out;
91
+ }
92
+ export function peopleOut(m, signed, withTokens, said) {
93
+ const people = {};
94
+ const about = {};
95
+ const describe = (name, p) => {
96
+ about[name] = {
97
+ real: p?.real ?? 'local',
98
+ default: m.defaults.person === name || (p?.default ?? false),
99
+ describe: p?.describe ?? null,
100
+ signedIn: signed?.tokens[name] !== undefined,
101
+ };
102
+ };
103
+ for (const p of Object.values(m.people))
104
+ describe(p.name, p);
105
+ for (const [name, token] of Object.entries(signed?.tokens ?? {})) {
106
+ if (!(name in about))
107
+ describe(name, null);
108
+ if (withTokens)
109
+ people[name] = { ...signed.fields[name], token };
110
+ }
111
+ return {
112
+ v: 1,
113
+ people,
114
+ variables: signed?.variables ?? {},
115
+ about,
116
+ signedIn: signed !== null && Object.keys(signed.tokens).length > 0,
117
+ said,
118
+ };
119
+ }
120
+ export function scrub(text, tokens) {
121
+ let out = text;
122
+ const longestFirst = Object.entries(tokens).sort(([, a], [, b]) => b.length - a.length);
123
+ for (const [name, token] of longestFirst)
124
+ if (token.length >= 6)
125
+ out = out.split(token).join(`{{${name}}}`);
126
+ return out;
127
+ }
128
+ export function scrubDeep(value, tokens) {
129
+ if (Object.keys(tokens).length === 0)
130
+ return value;
131
+ const walk = (v) => typeof v === 'string'
132
+ ? scrub(v, tokens)
133
+ : Array.isArray(v)
134
+ ? v.map(walk)
135
+ : v && typeof v === 'object'
136
+ ? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, walk(x)]))
137
+ : v;
138
+ return walk(value);
139
+ }
@@ -0,0 +1,20 @@
1
+ import type { KindRunner } from '../kinds/index.ts';
2
+ import { type Manifest } from '../manifest/index.ts';
3
+ import type { Scenario } from '../scenarios/index.ts';
4
+ import type { Observation } from './observation.ts';
5
+ export declare function hashOfContents(at: string): string;
6
+ export declare function manifestPartsUsedBy(m: Manifest, services: readonly string[], world: string | null, kind: string): unknown;
7
+ export declare function groupsOf(m: Manifest, scenarios: readonly Scenario[], world: string | null): Promise<Map<string, string>>;
8
+ export interface KeyParts {
9
+ group?: string | null;
10
+ commit: string;
11
+ scenario: Scenario;
12
+ world: string | null;
13
+ person: string | null;
14
+ real: readonly string[];
15
+ runner: KindRunner;
16
+ manifest: Manifest;
17
+ }
18
+ export declare function cacheKeyOf(p: KeyParts): string;
19
+ export declare function cached(m: Manifest, commit: string, key: string): Observation | null;
20
+ export declare function keep(m: Manifest, o: Observation): boolean;
@@ -0,0 +1,98 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
3
+ import path from 'node:path';
4
+ import { hashOf } from '../manifest/index.js';
5
+ import { stateDir } from '../services/index.js';
6
+ import { chainOf } from '../worlds/index.js';
7
+ export function hashOfContents(at) {
8
+ const hash = createHash('sha256');
9
+ const walk = (p, rel) => {
10
+ if (!existsSync(p)) {
11
+ hash.update(`missing ${rel}\n`);
12
+ return;
13
+ }
14
+ if (statSync(p).isDirectory())
15
+ for (const name of readdirSync(p).sort())
16
+ walk(path.join(p, name), `${rel}/${name}`);
17
+ else
18
+ hash.update(`${rel}\n`).update(readFileSync(p));
19
+ };
20
+ walk(at, '.');
21
+ return hash.digest('hex').slice(0, 16);
22
+ }
23
+ const settingsWithoutPaths = (block) => {
24
+ const { paths: _paths, ...rest } = block;
25
+ return rest;
26
+ };
27
+ export function manifestPartsUsedBy(m, services, world, kind) {
28
+ const kinds = {
29
+ call: settingsWithoutPaths(m.scenarios.calls),
30
+ piece: settingsWithoutPaths(m.scenarios.pieces),
31
+ flow: settingsWithoutPaths(m.scenarios.flows),
32
+ };
33
+ return {
34
+ services: services.map((s) => m.services[s] ?? null),
35
+ world: world && m.worlds[world] ? chainOf(m, world) : null,
36
+ standins: Object.values(m.standins).filter((x) => x.services === null || x.services.some((s) => services.includes(s))),
37
+ prepare: m.prepare,
38
+ signIn: m.signIn,
39
+ kind: kinds[kind] ?? null,
40
+ overlay: m.overlay.map((o) => [o.to, hashOfContents(o.from)]),
41
+ };
42
+ }
43
+ export async function groupsOf(m, scenarios, world) {
44
+ const { runnerOf } = await import('../kinds/index.js');
45
+ const groups = new Map();
46
+ for (const s of scenarios) {
47
+ const services = (await runnerOf(s.kind)).services(s, m);
48
+ if (services.length === 0)
49
+ continue;
50
+ const sameServicesInTheSameWorld = `${world ?? s.world ?? ''}|${[...services].sort().join(',')}`;
51
+ groups.set(sameServicesInTheSameWorld, [...(groups.get(sameServicesInTheSameWorld) ?? []), s]);
52
+ }
53
+ const out = new Map();
54
+ for (const members of groups.values()) {
55
+ const hash = hashOf(members.map((s) => [s.id, s.definition, s.context ?? null]));
56
+ for (const s of members)
57
+ out.set(s.id, hash);
58
+ }
59
+ return out;
60
+ }
61
+ export function cacheKeyOf(p) {
62
+ const services = p.runner.services(p.scenario, p.manifest);
63
+ return hashOf({
64
+ commit: p.commit,
65
+ id: p.scenario.id,
66
+ definition: p.scenario.definition,
67
+ context: p.scenario.context ?? null,
68
+ group: p.group ?? null,
69
+ world: p.world,
70
+ person: p.person,
71
+ real: [...p.real].sort(),
72
+ runner: p.runner.version,
73
+ manifest: manifestPartsUsedBy(p.manifest, services, p.world, p.scenario.kind),
74
+ });
75
+ }
76
+ const fileOf = (m, commit, key) => path.join(stateDir(m.dir), 'cache', commit.slice(0, 12), `${key}.json`);
77
+ export function cached(m, commit, key) {
78
+ const file = fileOf(m, commit, key);
79
+ if (!existsSync(file))
80
+ return null;
81
+ try {
82
+ return { ...JSON.parse(readFileSync(file, 'utf8')), cached: true };
83
+ }
84
+ catch {
85
+ return null;
86
+ }
87
+ }
88
+ const didntAnswer = (o) => o.failures.some((f) => typeof f.got === 'string' && /^It didn’t answer|timed out/.test(f.got));
89
+ export function keep(m, o) {
90
+ if (!o.cacheKey || !o.checkout.commit || o.checkout.dirty || o.broke)
91
+ return false;
92
+ if (didntAnswer(o))
93
+ return false;
94
+ const file = fileOf(m, o.checkout.commit, o.cacheKey);
95
+ mkdirSync(path.dirname(file), { recursive: true });
96
+ writeFileSync(file, `${JSON.stringify(o)}\n`);
97
+ return true;
98
+ }
@@ -0,0 +1,5 @@
1
+ export { cached, cacheKeyOf, groupsOf, keep, type KeyParts } from './cache.ts';
2
+ export type { CheckoutInfo, Observation } from './observation.ts';
3
+ export { slotsFor, type Slots } from './parallel.ts';
4
+ export { applyWorld, placedIn, runSide, runSideSharing, type NotRun, type SessionOptions, type SideRun, } from './session.ts';
5
+ export { baseVars } from './vars.ts';
@@ -0,0 +1,4 @@
1
+ export { cached, cacheKeyOf, groupsOf, keep } from './cache.js';
2
+ export { slotsFor } from './parallel.js';
3
+ export { applyWorld, placedIn, runSide, runSideSharing, } from './session.js';
4
+ export { baseVars } from './vars.js';
@@ -0,0 +1,27 @@
1
+ import type { Artifact, Broke, Failure, StandinUse } from '../kinds/index.ts';
2
+ import type { ScenarioRef } from '../scenarios/index.ts';
3
+ export interface CheckoutInfo {
4
+ path: string;
5
+ ref: string | null;
6
+ commit: string | null;
7
+ dirty: boolean;
8
+ }
9
+ export interface Observation {
10
+ v: 1;
11
+ ok: boolean;
12
+ scenario: ScenarioRef;
13
+ checkout: CheckoutInfo;
14
+ world: string | null;
15
+ person: string | null;
16
+ standins: StandinUse[];
17
+ failures: Failure[];
18
+ broke: Broke | null;
19
+ observed: Record<string, unknown> | null;
20
+ artifacts: Artifact[];
21
+ ms: number;
22
+ at: string;
23
+ rehearse: string;
24
+ runner: string;
25
+ cacheKey: string | null;
26
+ cached: boolean;
27
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ import type { KindRunner } from '../kinds/index.ts';
2
+ import type { Kind, Manifest } from '../manifest/index.ts';
3
+ import type { Scenario } from '../scenarios/index.ts';
4
+ export interface Slots {
5
+ take(): Promise<() => void>;
6
+ }
7
+ export declare function slotsFor(n: number): Slots;
8
+ export interface Unit {
9
+ at: number;
10
+ world: string | null;
11
+ members: Scenario[];
12
+ runners: Map<Kind, KindRunner>;
13
+ needed: Set<string>;
14
+ vouched: boolean;
15
+ holds: ReadonlySet<string>;
16
+ holdsTheWholeCheckout: boolean;
17
+ }
18
+ export interface WorldToSplit {
19
+ at: number;
20
+ world: string | null;
21
+ group: readonly Scenario[];
22
+ runners: ReadonlyMap<Kind, KindRunner>;
23
+ needed: ReadonlySet<string>;
24
+ vouched: boolean;
25
+ servicesOf(s: Scenario): string[];
26
+ }
27
+ export declare function unitsOf(m: Manifest, w: WorldToSplit): Unit[];
28
+ export declare function inTurn<T>(units: readonly Unit[], slots: Slots, work: (u: Unit) => Promise<T>, skip: (u: Unit) => boolean): Promise<(T | null)[]>;
@@ -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
+ }
@@ -0,0 +1,35 @@
1
+ import { type Scenario } from '../scenarios/index.ts';
2
+ import { type StandinUse } from '../kinds/index.ts';
3
+ import { type Manifest } from '../manifest/index.ts';
4
+ import { applyWorld } from '../worlds/index.ts';
5
+ export { applyWorld };
6
+ import type { CheckoutInfo, Observation } from './observation.ts';
7
+ import { type Slots } from './parallel.ts';
8
+ export interface SessionOptions {
9
+ world: string | null;
10
+ as: string | null;
11
+ real: ReadonlySet<string>;
12
+ keepUp: boolean;
13
+ progress: (line: string) => void;
14
+ ref: string | null;
15
+ groups?: ReadonlyMap<string, string>;
16
+ parallel?: number;
17
+ }
18
+ export interface NotRun {
19
+ scenario: Scenario;
20
+ step: string;
21
+ says: string;
22
+ }
23
+ export interface SideRun {
24
+ checkout: CheckoutInfo;
25
+ observations: Map<string, Observation>;
26
+ broke: {
27
+ step: string;
28
+ says: string;
29
+ } | null;
30
+ notRun: NotRun[];
31
+ }
32
+ export declare function standinUses(m: Manifest, services: readonly string[], real: ReadonlySet<string>): StandinUse[];
33
+ export declare function placedIn<T>(value: T, folders: readonly [string, string][]): T;
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>;