@kurvball/rehearse 1.2.1 → 1.4.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 (46) hide show
  1. package/README.md +9 -3
  2. package/dist/commands/clean.js +2 -2
  3. package/dist/commands/command.d.ts +3 -1
  4. package/dist/commands/command.js +18 -1
  5. package/dist/commands/compare.js +9 -3
  6. package/dist/commands/run.js +5 -3
  7. package/dist/commands/stage.js +9 -4
  8. package/dist/compare/announce.d.ts +1 -0
  9. package/dist/compare/changesets.d.ts +8 -0
  10. package/dist/compare/changesets.js +91 -0
  11. package/dist/compare/compare.d.ts +2 -0
  12. package/dist/compare/compare.js +44 -12
  13. package/dist/compare/index.d.ts +2 -0
  14. package/dist/compare/index.js +2 -0
  15. package/dist/drive/daemon.js +2 -1
  16. package/dist/git/checkout.js +8 -2
  17. package/dist/git/seed.d.ts +3 -0
  18. package/dist/git/seed.js +115 -0
  19. package/dist/kinds/flow/kind.js +27 -15
  20. package/dist/kinds/index.d.ts +1 -1
  21. package/dist/kinds/kind.d.ts +6 -0
  22. package/dist/kinds/piece/conventions.d.ts +1 -1
  23. package/dist/kinds/piece/conventions.js +1 -2
  24. package/dist/kinds/piece/kind.js +2 -0
  25. package/dist/kinds/piece/server.d.ts +1 -0
  26. package/dist/kinds/piece/server.js +1 -1
  27. package/dist/manifest/load.js +1 -0
  28. package/dist/manifest/types.d.ts +2 -0
  29. package/dist/run/index.d.ts +2 -1
  30. package/dist/run/index.js +2 -1
  31. package/dist/run/parallel.d.ts +28 -0
  32. package/dist/run/parallel.js +140 -0
  33. package/dist/run/session.d.ts +3 -0
  34. package/dist/run/session.js +312 -171
  35. package/dist/services/index.d.ts +1 -1
  36. package/dist/services/index.js +1 -1
  37. package/dist/services/service.d.ts +2 -1
  38. package/dist/services/service.js +41 -12
  39. package/dist/stage/api.d.ts +3 -0
  40. package/dist/stage/api.js +3 -2
  41. package/dist/stage/server.d.ts +3 -0
  42. package/dist/stage/server.js +5 -1
  43. package/llms.txt +3 -3
  44. package/package.json +1 -1
  45. package/schema/v1/manifest.schema.json +4 -0
  46. package/skill/rehearse/SKILL.md +4 -0
@@ -10,7 +10,7 @@ import { readScenarios, registerReader } from '../../scenarios/index.js';
10
10
  import { validate } from '../../schema.js';
11
11
  import { broken, registerRunner } from '../kind.js';
12
12
  import { startPieceServer } from '../piece/server.js';
13
- import { restart, restartable } from '../../services/index.js';
13
+ import { restart, restartable, stateDir } from '../../services/index.js';
14
14
  import { applyWorld } from '../../worlds/index.js';
15
15
  import { defaultExportOf, LiteralError } from './literal.js';
16
16
  const refuse = (says, line) => {
@@ -271,27 +271,35 @@ async function throughBrowser(s, ctx, browser, url) {
271
271
  await context.close();
272
272
  }
273
273
  }
274
- async function restartWhereTheWorldStarts(ctx, running) {
274
+ async function upAgainWhereTheWorldStarts(ctx, running) {
275
275
  const vars = { ...ctx.variables, ...(ctx.world && { world: ctx.world }) };
276
276
  try {
277
- if (ctx.world)
278
- await applyWorld(ctx.manifest, ctx.world, 'before', ctx.checkout, vars);
279
- const why = await restart(ctx.manifest, running);
277
+ const why = await restart(ctx.manifest, running, async () => {
278
+ if (ctx.world)
279
+ await applyWorld(ctx.manifest, ctx.world, 'before', ctx.checkout, vars);
280
+ });
280
281
  if (why)
281
- return broken('driver', why);
282
+ return { stopped: broken('driver', why) };
282
283
  if (ctx.world)
283
284
  await applyWorld(ctx.manifest, ctx.world, 'add', ctx.checkout, vars);
284
- return null;
285
285
  }
286
286
  catch (error) {
287
- return broken('world', errorText(error));
287
+ return { stopped: broken('world', errorText(error)) };
288
+ }
289
+ try {
290
+ return { signedInAgain: { ...ctx, ...(await ctx.signInAgain()) } };
291
+ }
292
+ catch (error) {
293
+ return { stopped: broken('signIn', scrub(errorText(error), ctx.tokens)) };
288
294
  }
289
295
  }
296
+ const cantBeBroughtUpAfresh = (service, afterAnotherFlow) => `${service} can't be brought up afresh (it needs a down command, and an up that hands over to a daemon), so this flow started ${afterAnotherFlow ? 'where the last one left the app' : 'with the app as it found it'}.`;
290
297
  registerRunner({
291
298
  kind: 'flow',
292
- version: 'flow/2',
299
+ version: 'flow/3',
293
300
  services: (s) => (s.service ? [s.service] : []),
294
- async run(scenarios, ctx) {
301
+ async run(scenarios, asGiven) {
302
+ let ctx = asGiven;
295
303
  const out = new Map();
296
304
  let browser = null;
297
305
  const pieces = new Map();
@@ -302,16 +310,19 @@ registerRunner({
302
310
  const svc = s.service ? ctx.manifest.services[s.service] : undefined;
303
311
  if (svc?.drive) {
304
312
  const running = ctx.running.find((r) => r.name === svc.name);
313
+ const afterAnotherFlow = driven.has(svc.name);
314
+ const freshAlready = !afterAnotherFlow && ctx.broughtUpAndUnused.has(svc.name);
305
315
  let shared = null;
306
- if (driven.has(svc.name) && running) {
316
+ if (running && !freshAlready) {
307
317
  if (!restartable(running))
308
- shared = `${svc.name} can't be brought up afresh (it needs a down command, and an up that hands over to a daemon), so this flow started where the last one left the app.`;
318
+ shared = cantBeBroughtUpAfresh(svc.name, afterAnotherFlow);
309
319
  else {
310
- const stopped = await restartWhereTheWorldStarts(ctx, running);
311
- if (stopped) {
312
- out.set(s.id, stopped);
320
+ const again = await upAgainWhereTheWorldStarts(ctx, running);
321
+ if ('stopped' in again) {
322
+ out.set(s.id, again.stopped);
313
323
  continue;
314
324
  }
325
+ ctx = again.signedInAgain;
315
326
  }
316
327
  }
317
328
  driven.add(svc.name);
@@ -344,6 +355,7 @@ registerRunner({
344
355
  try {
345
356
  server = await startPieceServer({
346
357
  dir: pspec.packageDir,
358
+ state: stateDir(ctx.manifest.dir),
347
359
  config: ctx.manifest.scenarios.pieces.config ?? null,
348
360
  styles: ctx.manifest.scenarios.pieces.styles ?? null,
349
361
  preview: ctx.manifest.scenarios.pieces.preview ?? null,
@@ -1,4 +1,4 @@
1
- export { broken, loadKinds, runnerOf, type Artifact, type Broke, type Failure, type KindRunner, type Ran, type StandinUse, } from './kind.ts';
1
+ export { broken, loadKinds, runnerOf, type Artifact, type Broke, type Failure, type KindRunner, type Ran, type RunContext, type SignedInAgain, type StandinUse, } from './kind.ts';
2
2
  export { callDefinition } from './call/definition.ts';
3
3
  export { parseHurl, Refusal } from './call/hurl.ts';
4
4
  export { sendFile, type Fetcher } from './call/send.ts';
@@ -32,6 +32,10 @@ export interface StandinUse {
32
32
  fidelity: string;
33
33
  real: boolean;
34
34
  }
35
+ export interface SignedInAgain {
36
+ tokens: Record<string, string>;
37
+ variables: Record<string, string>;
38
+ }
35
39
  export interface RunContext {
36
40
  manifest: Manifest;
37
41
  checkout: string;
@@ -40,6 +44,8 @@ export interface RunContext {
40
44
  tokens: Record<string, string>;
41
45
  variables: Record<string, string>;
42
46
  running: Running[];
47
+ broughtUpAndUnused: ReadonlySet<string>;
48
+ signInAgain(): Promise<SignedInAgain>;
43
49
  keep: string;
44
50
  progress(line: string): void;
45
51
  }
@@ -5,7 +5,7 @@ export interface Found {
5
5
  }
6
6
  export declare const CONFIG_CONVENTIONS: readonly string[];
7
7
  export declare const STYLES_CONVENTIONS: readonly string[];
8
- export declare const cacheDirOf: (dir: string, under?: string) => string;
8
+ export declare const cacheDirOf: (dir: string, state: string) => string;
9
9
  export declare const SKIPPED_FOLDERS: ReadonlySet<string>;
10
10
  export declare function findConfig(dir: string, given?: string | null): Found | null;
11
11
  export declare function findStyles(dir: string, given?: string | null): Found | null;
@@ -1,12 +1,11 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
3
- import { tmpdir } from 'node:os';
4
3
  import path from 'node:path';
5
4
  import { PIECE_FILE } from './csf.js';
6
5
  const EXTENSIONS = ['ts', 'mts', 'js', 'mjs', 'cts', 'cjs'];
7
6
  export const CONFIG_CONVENTIONS = ['vite', 'vitest'].flatMap((name) => EXTENSIONS.map((ext) => `${name}.config.${ext}`));
8
7
  export const STYLES_CONVENTIONS = ['src/styles.css', 'src/index.css'];
9
- export const cacheDirOf = (dir, under = tmpdir()) => path.join(under, 'rehearse-pieces', createHash('sha256').update(path.resolve(dir)).digest('hex').slice(0, 16));
8
+ export const cacheDirOf = (dir, state) => path.join(state, 'pieces', createHash('sha256').update(path.resolve(dir)).digest('hex').slice(0, 16));
10
9
  export const SKIPPED_FOLDERS = new Set(['node_modules', 'dist', 'out']);
11
10
  const isFile = (file) => {
12
11
  try {
@@ -7,6 +7,7 @@ import { calm, contextFor, errorsOf, FIXED_TIME, launch } from '../../drive/inde
7
7
  import { errorText } from '../../errors.js';
8
8
  import { hashOf } from '../../manifest/index.js';
9
9
  import { registerReader, toPosix } from '../../scenarios/index.js';
10
+ import { stateDir } from '../../services/index.js';
10
11
  import { broken, registerRunner } from '../kind.js';
11
12
  import { ROOT_ID, THREW } from './canvas.js';
12
13
  import { metaTitle, propertyString, statementAt, storyExportsOf, storyNameFromExport, titleFromPath, toId, } from './csf.js';
@@ -177,6 +178,7 @@ export async function runPieces(scenarios, ctx, browser) {
177
178
  try {
178
179
  server = await startPieceServer({
179
180
  dir,
181
+ state: stateDir(m.dir),
180
182
  config: settings.config ?? null,
181
183
  styles: settings.styles ?? null,
182
184
  preview: settings.preview ?? null,
@@ -3,6 +3,7 @@ export declare const HOST = "127.0.0.1";
3
3
  export declare const resolvable: (dir: string, request: string) => boolean;
4
4
  export interface PieceServerOptions {
5
5
  dir: string;
6
+ state: string;
6
7
  config?: string | null;
7
8
  styles?: string | null;
8
9
  preview?: string | null;
@@ -164,7 +164,7 @@ export async function startPieceServer(o) {
164
164
  const inline = {
165
165
  configFile: config?.file ?? false,
166
166
  root: dir,
167
- cacheDir: cacheDirOf(dir),
167
+ cacheDir: cacheDirOf(dir, o.state),
168
168
  base: '/',
169
169
  mode: 'development',
170
170
  clearScreen: false,
@@ -195,6 +195,7 @@ export function manifestFrom(text, file) {
195
195
  base: raw.base ?? 'main',
196
196
  checkout: raw.checkout ? path.resolve(dir, raw.checkout) : null,
197
197
  prepare: raw.prepare ?? null,
198
+ seed: raw.seed ?? true,
198
199
  overlay: (raw.overlay ?? []).map((o) => ({ from: path.resolve(dir, o.from), to: o.to })),
199
200
  services,
200
201
  signIn,
@@ -3,6 +3,7 @@ export interface RawManifest {
3
3
  base?: string;
4
4
  checkout?: string;
5
5
  prepare?: string;
6
+ seed?: boolean;
6
7
  overlay?: {
7
8
  from: string;
8
9
  to: string;
@@ -222,6 +223,7 @@ export interface Manifest {
222
223
  base: string;
223
224
  checkout: string | null;
224
225
  prepare: string | null;
226
+ seed: boolean;
225
227
  overlay: {
226
228
  from: string;
227
229
  to: string;
@@ -1,4 +1,5 @@
1
1
  export { cached, cacheKeyOf, groupsOf, keep, type KeyParts } from './cache.ts';
2
2
  export type { CheckoutInfo, Observation } from './observation.ts';
3
- export { applyWorld, placedIn, runSide, type NotRun, type SessionOptions, type SideRun } from './session.ts';
3
+ export { slotsFor, type Slots } from './parallel.ts';
4
+ export { applyWorld, placedIn, runSide, runSideSharing, type NotRun, type SessionOptions, type SideRun, } from './session.ts';
4
5
  export { baseVars } from './vars.ts';
package/dist/run/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { cached, cacheKeyOf, groupsOf, keep } from './cache.js';
2
- export { applyWorld, placedIn, runSide } from './session.js';
2
+ export { slotsFor } from './parallel.js';
3
+ export { applyWorld, placedIn, runSide, runSideSharing, } from './session.js';
3
4
  export { baseVars } from './vars.js';
@@ -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
+ }
@@ -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>;