@kurvball/rehearse 0.0.0-stage → 1.2.1

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 (266) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +102 -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 +29 -0
  16. package/dist/commands/command.js +29 -0
  17. package/dist/commands/compare.d.ts +4 -0
  18. package/dist/commands/compare.js +84 -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 +82 -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 +49 -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 +82 -0
  46. package/dist/compare/compare.js +310 -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 +4 -0
  50. package/dist/compare/index.js +4 -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 +84 -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/index.d.ts +18 -0
  80. package/dist/index.js +15 -0
  81. package/dist/kinds/all.d.ts +4 -0
  82. package/dist/kinds/all.js +4 -0
  83. package/dist/kinds/call/check.d.ts +29 -0
  84. package/dist/kinds/call/check.js +187 -0
  85. package/dist/kinds/call/definition.d.ts +16 -0
  86. package/dist/kinds/call/definition.js +33 -0
  87. package/dist/kinds/call/hurl.d.ts +65 -0
  88. package/dist/kinds/call/hurl.js +333 -0
  89. package/dist/kinds/call/kind.d.ts +8 -0
  90. package/dist/kinds/call/kind.js +122 -0
  91. package/dist/kinds/call/send.d.ts +32 -0
  92. package/dist/kinds/call/send.js +152 -0
  93. package/dist/kinds/call/words.d.ts +22 -0
  94. package/dist/kinds/call/words.js +24 -0
  95. package/dist/kinds/file/kind.d.ts +44 -0
  96. package/dist/kinds/file/kind.js +331 -0
  97. package/dist/kinds/file/layouts.d.ts +14 -0
  98. package/dist/kinds/file/layouts.js +103 -0
  99. package/dist/kinds/flow/kind.d.ts +21 -0
  100. package/dist/kinds/flow/kind.js +401 -0
  101. package/dist/kinds/flow/literal.d.ts +8 -0
  102. package/dist/kinds/flow/literal.js +148 -0
  103. package/dist/kinds/index.d.ts +10 -0
  104. package/dist/kinds/index.js +8 -0
  105. package/dist/kinds/kind.d.ts +57 -0
  106. package/dist/kinds/kind.js +21 -0
  107. package/dist/kinds/piece/canvas.d.ts +18 -0
  108. package/dist/kinds/piece/canvas.js +165 -0
  109. package/dist/kinds/piece/conventions.d.ts +12 -0
  110. package/dist/kinds/piece/conventions.js +101 -0
  111. package/dist/kinds/piece/csf.d.ts +10 -0
  112. package/dist/kinds/piece/csf.js +304 -0
  113. package/dist/kinds/piece/kind.d.ts +12 -0
  114. package/dist/kinds/piece/kind.js +297 -0
  115. package/dist/kinds/piece/server.d.ts +23 -0
  116. package/dist/kinds/piece/server.js +236 -0
  117. package/dist/manifest/fill.d.ts +4 -0
  118. package/dist/manifest/fill.js +7 -0
  119. package/dist/manifest/index.d.ts +5 -0
  120. package/dist/manifest/index.js +5 -0
  121. package/dist/manifest/init.d.ts +1 -0
  122. package/dist/manifest/init.js +47 -0
  123. package/dist/manifest/load.d.ts +7 -0
  124. package/dist/manifest/load.js +244 -0
  125. package/dist/manifest/types.d.ts +253 -0
  126. package/dist/manifest/types.js +7 -0
  127. package/dist/manifest/yaml.d.ts +5 -0
  128. package/dist/manifest/yaml.js +47 -0
  129. package/dist/people/index.d.ts +1 -0
  130. package/dist/people/index.js +1 -0
  131. package/dist/people/people.d.ts +39 -0
  132. package/dist/people/people.js +139 -0
  133. package/dist/run/cache.d.ts +20 -0
  134. package/dist/run/cache.js +98 -0
  135. package/dist/run/index.d.ts +4 -0
  136. package/dist/run/index.js +3 -0
  137. package/dist/run/observation.d.ts +27 -0
  138. package/dist/run/observation.js +1 -0
  139. package/dist/run/session.d.ts +32 -0
  140. package/dist/run/session.js +258 -0
  141. package/dist/run/vars.d.ts +3 -0
  142. package/dist/run/vars.js +13 -0
  143. package/dist/scenarios/area.d.ts +6 -0
  144. package/dist/scenarios/area.js +37 -0
  145. package/dist/scenarios/files.d.ts +9 -0
  146. package/dist/scenarios/files.js +26 -0
  147. package/dist/scenarios/glob.d.ts +6 -0
  148. package/dist/scenarios/glob.js +106 -0
  149. package/dist/scenarios/index.d.ts +4 -0
  150. package/dist/scenarios/index.js +4 -0
  151. package/dist/scenarios/read.d.ts +18 -0
  152. package/dist/scenarios/read.js +54 -0
  153. package/dist/scenarios/scenario.d.ts +34 -0
  154. package/dist/scenarios/scenario.js +10 -0
  155. package/dist/schema.d.ts +14 -0
  156. package/dist/schema.js +122 -0
  157. package/dist/services/index.d.ts +2 -0
  158. package/dist/services/index.js +2 -0
  159. package/dist/services/registry.d.ts +23 -0
  160. package/dist/services/registry.js +49 -0
  161. package/dist/services/service.d.ts +22 -0
  162. package/dist/services/service.js +284 -0
  163. package/dist/stage/api.d.ts +28 -0
  164. package/dist/stage/api.js +206 -0
  165. package/dist/stage/client/client/api.js +11 -0
  166. package/dist/stage/client/client/index.js +1 -0
  167. package/dist/stage/client/features/canvas/call.js +65 -0
  168. package/dist/stage/client/features/canvas/context.js +1 -0
  169. package/dist/stage/client/features/canvas/file.js +36 -0
  170. package/dist/stage/client/features/canvas/flow.js +59 -0
  171. package/dist/stage/client/features/canvas/index.js +1 -0
  172. package/dist/stage/client/features/canvas/observed.js +8 -0
  173. package/dist/stage/client/features/canvas/parts.js +24 -0
  174. package/dist/stage/client/features/canvas/piece.js +31 -0
  175. package/dist/stage/client/features/canvas/reach.js +6 -0
  176. package/dist/stage/client/features/canvas/views.js +16 -0
  177. package/dist/stage/client/features/list/index.js +1 -0
  178. package/dist/stage/client/features/list/list.js +53 -0
  179. package/dist/stage/client/features/list/reach.js +10 -0
  180. package/dist/stage/client/features/scenario/heading.js +14 -0
  181. package/dist/stage/client/features/scenario/index.js +1 -0
  182. package/dist/stage/client/features/scenario/reach.js +9 -0
  183. package/dist/stage/client/features/scenario/scenario.js +145 -0
  184. package/dist/stage/client/features/try/index.js +1 -0
  185. package/dist/stage/client/features/try/reach.js +12 -0
  186. package/dist/stage/client/features/try/try.js +65 -0
  187. package/dist/stage/client/index.html +16 -0
  188. package/dist/stage/client/main.js +24 -0
  189. package/dist/stage/client/primitives/busy.js +6 -0
  190. package/dist/stage/client/primitives/focus.js +21 -0
  191. package/dist/stage/client/primitives/h.js +25 -0
  192. package/dist/stage/client/primitives/index.js +6 -0
  193. package/dist/stage/client/primitives/keyed.js +16 -0
  194. package/dist/stage/client/primitives/say.js +6 -0
  195. package/dist/stage/client/primitives/state.js +21 -0
  196. package/dist/stage/client/primitives/types.js +1 -0
  197. package/dist/stage/client/primitives/verdicts.js +8 -0
  198. package/dist/stage/client/stage.css +652 -0
  199. package/dist/stage/compared.d.ts +20 -0
  200. package/dist/stage/compared.js +58 -0
  201. package/dist/stage/files.d.ts +4 -0
  202. package/dist/stage/files.js +59 -0
  203. package/dist/stage/guard.d.ts +3 -0
  204. package/dist/stage/guard.js +20 -0
  205. package/dist/stage/http.d.ts +12 -0
  206. package/dist/stage/http.js +34 -0
  207. package/dist/stage/index.d.ts +1 -0
  208. package/dist/stage/index.js +1 -0
  209. package/dist/stage/sending.d.ts +18 -0
  210. package/dist/stage/sending.js +66 -0
  211. package/dist/stage/server.d.ts +31 -0
  212. package/dist/stage/server.js +250 -0
  213. package/dist/version.d.ts +2 -0
  214. package/dist/version.js +5 -0
  215. package/dist/worlds/define.d.ts +16 -0
  216. package/dist/worlds/define.js +45 -0
  217. package/dist/worlds/index.d.ts +2 -0
  218. package/dist/worlds/index.js +2 -0
  219. package/dist/worlds/worlds.d.ts +15 -0
  220. package/dist/worlds/worlds.js +46 -0
  221. package/llms.txt +46 -0
  222. package/package.json +84 -3
  223. package/schema/v1/announce.schema.json +28 -0
  224. package/schema/v1/check.schema.json +67 -0
  225. package/schema/v1/clean.schema.json +19 -0
  226. package/schema/v1/common.schema.json +210 -0
  227. package/schema/v1/comparison.schema.json +298 -0
  228. package/schema/v1/dependents.schema.json +30 -0
  229. package/schema/v1/drive.schema.json +112 -0
  230. package/schema/v1/error.schema.json +26 -0
  231. package/schema/v1/file-scenario.schema.json +175 -0
  232. package/schema/v1/flow.schema.json +216 -0
  233. package/schema/v1/init.schema.json +20 -0
  234. package/schema/v1/list.schema.json +70 -0
  235. package/schema/v1/manifest.schema.json +430 -0
  236. package/schema/v1/observation.schema.json +388 -0
  237. package/schema/v1/open.schema.json +14 -0
  238. package/schema/v1/people.schema.json +88 -0
  239. package/schema/v1/samples/announce.yaml +5 -0
  240. package/schema/v1/samples/check.json +13 -0
  241. package/schema/v1/samples/clean.json +1 -0
  242. package/schema/v1/samples/comparison.json +83 -0
  243. package/schema/v1/samples/dependents.json +7 -0
  244. package/schema/v1/samples/drive.json +10 -0
  245. package/schema/v1/samples/error.json +10 -0
  246. package/schema/v1/samples/file-scenario.yaml +13 -0
  247. package/schema/v1/samples/flow.yaml +13 -0
  248. package/schema/v1/samples/init.json +1 -0
  249. package/schema/v1/samples/list.json +24 -0
  250. package/schema/v1/samples/manifest.yaml +55 -0
  251. package/schema/v1/samples/observation.json +49 -0
  252. package/schema/v1/samples/open.json +6 -0
  253. package/schema/v1/samples/people.json +11 -0
  254. package/schema/v1/samples/services.json +27 -0
  255. package/schema/v1/samples/stage.json +6 -0
  256. package/schema/v1/samples/try.json +13 -0
  257. package/schema/v1/samples/up.json +16 -0
  258. package/schema/v1/samples/validate.json +7 -0
  259. package/schema/v1/samples/worlds.json +21 -0
  260. package/schema/v1/services.schema.json +72 -0
  261. package/schema/v1/stage.schema.json +14 -0
  262. package/schema/v1/try.schema.json +56 -0
  263. package/schema/v1/up.schema.json +55 -0
  264. package/schema/v1/validate.schema.json +40 -0
  265. package/schema/v1/worlds.schema.json +33 -0
  266. 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,4 @@
1
+ export { cached, cacheKeyOf, groupsOf, keep, type KeyParts } from './cache.ts';
2
+ export type { CheckoutInfo, Observation } from './observation.ts';
3
+ export { applyWorld, placedIn, runSide, type NotRun, type SessionOptions, type SideRun } from './session.ts';
4
+ export { baseVars } from './vars.ts';
@@ -0,0 +1,3 @@
1
+ export { cached, cacheKeyOf, groupsOf, keep } from './cache.js';
2
+ export { applyWorld, placedIn, runSide } from './session.js';
3
+ 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,32 @@
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
+ export interface SessionOptions {
8
+ world: string | null;
9
+ as: string | null;
10
+ real: ReadonlySet<string>;
11
+ keepUp: boolean;
12
+ progress: (line: string) => void;
13
+ ref: string | null;
14
+ groups?: ReadonlyMap<string, string>;
15
+ }
16
+ export interface NotRun {
17
+ scenario: Scenario;
18
+ step: string;
19
+ says: string;
20
+ }
21
+ export interface SideRun {
22
+ checkout: CheckoutInfo;
23
+ observations: Map<string, Observation>;
24
+ broke: {
25
+ step: string;
26
+ says: string;
27
+ } | null;
28
+ notRun: NotRun[];
29
+ }
30
+ export declare function standinUses(m: Manifest, services: readonly string[], real: ReadonlySet<string>): StandinUse[];
31
+ export declare function placedIn<T>(value: T, folders: readonly [string, string][]): T;
32
+ export declare function runSide(m: Manifest, checkout: string, scenarios: readonly Scenario[], o: SessionOptions): Promise<SideRun>;
@@ -0,0 +1,258 @@
1
+ import { mkdirSync, realpathSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { infoOf, overlay, topOf } from '../git/index.js';
4
+ import { toPosix, refOf } from '../scenarios/index.js';
5
+ import { RehearseError, errorText } from '../errors.js';
6
+ import { broken, runnerOf } from '../kinds/index.js';
7
+ import { fill } from '../manifest/index.js';
8
+ import { signIn } from '../people/index.js';
9
+ import { stateDir, bringDown, bringUp, liveServices, makeRoom, release, standinsFor, } from '../services/index.js';
10
+ import { VERSION } from '../version.js';
11
+ import { applyWorld, chainOf } from '../worlds/index.js';
12
+ export { applyWorld };
13
+ import { cacheKeyOf, groupsOf } from './cache.js';
14
+ import { baseVars } from './vars.js';
15
+ const worldOf = (s, o) => o.world ?? s.world;
16
+ const personOf = (s, o) => o.as ?? s.person;
17
+ export function standinUses(m, services, real) {
18
+ const seen = new Map();
19
+ for (const svc of services)
20
+ for (const { standin, real: isReal } of standinsFor(m, svc, real))
21
+ seen.set(standin.name, {
22
+ name: standin.name,
23
+ replaces: standin.replaces,
24
+ by: standin.by,
25
+ fidelity: standin.fidelity,
26
+ real: isReal,
27
+ });
28
+ return [...seen.values()];
29
+ }
30
+ function everyServiceWhenTheWorldAdds(m, world) {
31
+ if (!world)
32
+ return [];
33
+ return chainOf(m, world).some((w) => w.add) ? Object.keys(m.services) : [];
34
+ }
35
+ const asItReallyIs = (dir) => {
36
+ try {
37
+ return realpathSync(dir);
38
+ }
39
+ catch {
40
+ return dir;
41
+ }
42
+ };
43
+ const wholeFolder = (dir) => new RegExp(`${dir.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?!\\w|[.-]\\w)`, 'g');
44
+ export function placedIn(value, folders) {
45
+ const asGivenAndAsTheyReallyAre = folders.flatMap(([dir, name]) => [
46
+ [dir, name],
47
+ [asItReallyIs(dir), name],
48
+ ]);
49
+ const longestFirst = [...new Map(asGivenAndAsTheyReallyAre)]
50
+ .filter(([dir]) => dir.length > 1)
51
+ .sort(([a], [b]) => b.length - a.length)
52
+ .map(([dir, name]) => [wholeFolder(dir), name]);
53
+ const walk = (v) => typeof v === 'string'
54
+ ? longestFirst.reduce((text, [dir, name]) => text.replace(dir, name), v)
55
+ : Array.isArray(v)
56
+ ? v.map(walk)
57
+ : v && typeof v === 'object'
58
+ ? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, walk(x)]))
59
+ : v;
60
+ return walk(value);
61
+ }
62
+ function manifestFolderAsOnBothSides(m) {
63
+ const home = path.resolve(m.checkout ?? topOf(m.dir) ?? m.dir);
64
+ const inside = path.relative(home, path.resolve(m.dir));
65
+ if (inside.startsWith('..') || path.isAbsolute(inside))
66
+ return '{{manifest}}';
67
+ return inside ? `{{checkout}}/${toPosix(inside)}` : '{{checkout}}';
68
+ }
69
+ export async function runSide(m, checkout, scenarios, o) {
70
+ const side = {
71
+ checkout: infoOf(m, checkout, o.ref),
72
+ observations: new Map(),
73
+ broke: null,
74
+ notRun: [],
75
+ };
76
+ if (scenarios.length === 0)
77
+ return side;
78
+ try {
79
+ overlay(m, checkout);
80
+ }
81
+ catch (error) {
82
+ side.broke = { step: 'overlay', says: errorText(error) };
83
+ return side;
84
+ }
85
+ side.checkout = infoOf(m, checkout, o.ref);
86
+ const groups = o.groups ?? (await groupsOf(m, scenarios, o.world));
87
+ const manifestAs = manifestFolderAsOnBothSides(m);
88
+ const placed = (v) => placedIn(v, [
89
+ [path.resolve(checkout), '{{checkout}}'],
90
+ [path.resolve(m.dir), manifestAs],
91
+ ]);
92
+ const keepDir = path.join(stateDir(m.dir), 'runs', `${Date.now().toString(36)}-${path.basename(checkout)}`);
93
+ mkdirSync(keepDir, { recursive: true });
94
+ const worlds = new Map();
95
+ for (const s of scenarios)
96
+ worlds.set(worldOf(s, o), [...(worlds.get(worldOf(s, o)) ?? []), s]);
97
+ for (const [world, group] of worlds) {
98
+ const runners = new Map();
99
+ for (const s of group)
100
+ if (!runners.has(s.kind))
101
+ runners.set(s.kind, await runnerOf(s.kind));
102
+ const needed = new Set([
103
+ ...group.flatMap((s) => runners.get(s.kind).services(s, m)),
104
+ ...everyServiceWhenTheWorldAdds(m, world),
105
+ ]);
106
+ if (m.signIn?.service)
107
+ needed.add(m.signIn.service);
108
+ const undeclared = [...needed].filter((name) => !m.services[name]);
109
+ if (undeclared.length) {
110
+ for (const s of group)
111
+ side.notRun.push({
112
+ scenario: s,
113
+ step: 'up',
114
+ says: `It needs the service ${undeclared.join(', ')}, which the manifest doesn't declare.`,
115
+ });
116
+ continue;
117
+ }
118
+ const started = [];
119
+ let running = await liveServices(m, checkout);
120
+ const vars = () => baseVars(m, checkout, running);
121
+ const leftUp = running.filter((r) => needed.has(r.name));
122
+ const rehearseCanVouchForTheirState = leftUp.length === 0;
123
+ const leftUpInAnotherWorld = leftUp.find((r) => (r.world ?? null) !== world);
124
+ if (leftUpInAnotherWorld) {
125
+ for (const s of group)
126
+ side.notRun.push({
127
+ scenario: s,
128
+ step: 'world',
129
+ says: `${leftUpInAnotherWorld.name} is already up in ${checkout} with ${leftUpInAnotherWorld.world ? `the world ${leftUpInAnotherWorld.world}` : 'no world Rehearse built'}, not ${world ?? 'no world'}: run with --world ${leftUpInAnotherWorld.world ?? '…'}, or stop it with \`rehearse down\`.`,
130
+ });
131
+ continue;
132
+ }
133
+ try {
134
+ const fresh = [...needed].filter((name) => !running.some((r) => r.name === name));
135
+ if (fresh.length)
136
+ await makeRoom(m, checkout, o.progress);
137
+ if (world && fresh.length)
138
+ await applyWorld(m, world, 'before', checkout, vars());
139
+ for (const name of Object.keys(m.services).filter((n) => fresh.includes(n))) {
140
+ const r = await bringUp(m, m.services[name], checkout, {
141
+ world,
142
+ real: o.real,
143
+ vars: vars(),
144
+ progress: o.progress,
145
+ });
146
+ started.push(r);
147
+ running = [...running, r];
148
+ }
149
+ }
150
+ catch (error) {
151
+ for (const r of started)
152
+ await bringDown(m, r, o.progress);
153
+ side.broke = {
154
+ step: error instanceof RehearseError && error.code === 'world' ? 'world' : 'up',
155
+ says: errorText(error),
156
+ };
157
+ return side;
158
+ }
159
+ const notRun = (step, says) => group.forEach((s) => side.notRun.push({ scenario: s, step, says }));
160
+ try {
161
+ if (world && started.length)
162
+ await applyWorld(m, world, 'add', checkout, vars());
163
+ let tokens = {};
164
+ let signedVars = {};
165
+ if (m.signIn || Object.values(m.people).some((p) => p.token)) {
166
+ const v = vars();
167
+ const signed = await signIn(m, {
168
+ cwd: checkout,
169
+ env: {},
170
+ fill: (t) => fill(t, { ...v, ...(world && { world }) }),
171
+ });
172
+ tokens = signed.tokens;
173
+ signedVars = signed.variables;
174
+ }
175
+ for (const [kind, runner] of runners) {
176
+ const these = group.filter((s) => s.kind === kind);
177
+ const begun = Date.now();
178
+ let ran;
179
+ try {
180
+ ran = await runner.run(these, {
181
+ manifest: m,
182
+ checkout,
183
+ world,
184
+ as: o.as,
185
+ tokens,
186
+ variables: { ...vars(), ...signedVars, ...(world && { world }) },
187
+ running,
188
+ keep: keepDir,
189
+ progress: o.progress,
190
+ });
191
+ }
192
+ catch (error) {
193
+ ran = new Map(these.map((s) => [s.id, broken('runner', errorText(error))]));
194
+ }
195
+ const ms = Math.round((Date.now() - begun) / Math.max(1, these.length));
196
+ for (const s of these) {
197
+ const r = ran.get(s.id);
198
+ if (!r)
199
+ continue;
200
+ if (r.broke &&
201
+ ['world', 'signIn', 'runner', 'driver', 'up'].includes(r.broke.step) &&
202
+ r.observed === null) {
203
+ side.notRun.push({ scenario: s, step: r.broke.step, says: r.broke.says });
204
+ continue;
205
+ }
206
+ const services = runner.services(s, m);
207
+ const person = personOf(s, o);
208
+ side.observations.set(s.id, {
209
+ v: 1,
210
+ ok: r.ok,
211
+ scenario: refOf(s),
212
+ checkout: side.checkout,
213
+ world,
214
+ person,
215
+ standins: standinUses(m, services, o.real),
216
+ failures: placed(r.failures),
217
+ broke: placed(r.broke),
218
+ observed: placed(r.observed),
219
+ artifacts: r.artifacts,
220
+ ms,
221
+ at: new Date().toISOString(),
222
+ rehearse: VERSION,
223
+ runner: runner.version,
224
+ cacheKey: side.checkout.commit && !side.checkout.dirty && rehearseCanVouchForTheirState
225
+ ? cacheKeyOf({
226
+ group: groups.get(s.id) ?? null,
227
+ commit: side.checkout.commit,
228
+ scenario: s,
229
+ world,
230
+ person,
231
+ real: [...o.real],
232
+ runner,
233
+ manifest: m,
234
+ })
235
+ : null,
236
+ cached: false,
237
+ });
238
+ }
239
+ }
240
+ }
241
+ catch (error) {
242
+ notRun(error instanceof RehearseError && error.code === 'sign_in'
243
+ ? 'signIn'
244
+ : error instanceof RehearseError && error.code === 'world'
245
+ ? 'world'
246
+ : 'runner', errorText(error));
247
+ }
248
+ finally {
249
+ if (o.keepUp)
250
+ for (const r of started)
251
+ release(r);
252
+ else
253
+ for (const r of started.toReversed())
254
+ await bringDown(m, r, o.progress);
255
+ }
256
+ }
257
+ return side;
258
+ }
@@ -0,0 +1,3 @@
1
+ import type { Manifest } from '../manifest/index.ts';
2
+ import type { Running } from '../services/index.ts';
3
+ export declare function baseVars(m: Manifest, checkout: string, running: readonly Running[]): Record<string, string>;
@@ -0,0 +1,13 @@
1
+ export function baseVars(m, checkout, running) {
2
+ const vars = { checkout, manifest: m.dir };
3
+ for (const r of running) {
4
+ if (r.url)
5
+ vars[`${r.name}_url`] = r.url;
6
+ if (r.port !== null)
7
+ vars[`${r.name}_port`] = String(r.port);
8
+ }
9
+ const main = m.defaults.service ? running.find((r) => r.name === m.defaults.service) : undefined;
10
+ if (main?.url)
11
+ vars.url = main.url;
12
+ return vars;
13
+ }
@@ -0,0 +1,6 @@
1
+ import type { Manifest } from '../manifest/index.ts';
2
+ import type { ScenarioFile } from './files.ts';
3
+ export declare const PACKAGE_FILES: readonly ['package.json', 'pyproject.toml', 'Cargo.toml', 'go.mod'];
4
+ export declare function packageDirOf(root: string, file: string): string | null;
5
+ export declare function packageNameOf(dir: string): string | null;
6
+ export declare function areaOf(m: Manifest, checkout: string, f: ScenarioFile): string;
@@ -0,0 +1,37 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { toPosix } from './glob.js';
4
+ export const PACKAGE_FILES = ['package.json', 'pyproject.toml', 'Cargo.toml', 'go.mod'];
5
+ export function packageDirOf(root, file) {
6
+ for (let at = path.dirname(path.resolve(root, file));; at = path.dirname(at)) {
7
+ if (PACKAGE_FILES.some((p) => existsSync(path.join(at, p))))
8
+ return at;
9
+ if (path.resolve(at) === path.resolve(root) || path.dirname(at) === at)
10
+ return null;
11
+ }
12
+ }
13
+ export function packageNameOf(dir) {
14
+ try {
15
+ const name = JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'))
16
+ .name;
17
+ if (typeof name === 'string' && name)
18
+ return name;
19
+ }
20
+ catch {
21
+ return null;
22
+ }
23
+ return null;
24
+ }
25
+ export function areaOf(m, checkout, f) {
26
+ if (f.from === 'manifest')
27
+ return toPosix(path.dirname(f.file)).replace(/^\.\/?/, '') || 'scenarios';
28
+ const dir = packageDirOf(checkout, f.file);
29
+ if (dir && path.resolve(dir) !== path.resolve(checkout))
30
+ return packageNameOf(dir) ?? toPosix(path.relative(checkout, dir));
31
+ const parts = f.file.split('/');
32
+ return parts.length > 2
33
+ ? parts.slice(0, 2).join('/')
34
+ : parts.length === 2
35
+ ? parts[0]
36
+ : path.basename(m.dir);
37
+ }
@@ -0,0 +1,9 @@
1
+ import { type Kind, type Manifest, type PathEntry } from '../manifest/index.ts';
2
+ export interface ScenarioFile {
3
+ kind: Kind;
4
+ file: string;
5
+ abs: string;
6
+ from: PathEntry['from'];
7
+ covers: string[];
8
+ }
9
+ export declare function filesOf(m: Manifest, kind: Kind, checkout: string): ScenarioFile[];