@kurvball/rehearse 1.13.1 → 1.13.3

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.
@@ -7,7 +7,7 @@ import { addToWalk, beginWalk, failed, driveArgv, exitOf, parseDrive, resultText
7
7
  import { sh, tail } from '../exec.js';
8
8
  import { fillEnv } from '../manifest/index.js';
9
9
  import { applyWorld, baseVars } from '../run/index.js';
10
- import { alive, runningServices, stateDir, bringDown, bringUp, release } from '../services/index.js';
10
+ import { alive, runningServices, stateDir, bringDown, bringUp, envOf, handoverSettled, release, } from '../services/index.js';
11
11
  const quote = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
12
12
  const here = fileURLToPath(import.meta.url);
13
13
  const daemonFile = path.join(path.dirname(here), '..', 'drive', `daemon${path.extname(here)}`);
@@ -77,6 +77,7 @@ Each answers in the drive result shape (schema/v1/drive.schema.json): 0 done, 1
77
77
  vars: baseVars(m, checkout, running),
78
78
  progress: ctx.progress,
79
79
  });
80
+ await handoverSettled(r);
80
81
  release(r);
81
82
  running = [...running, r];
82
83
  if (world)
@@ -196,10 +197,7 @@ Each answers in the drive result shape (schema/v1/drive.schema.json): 0 done, 1
196
197
  if (command.command === 'fill')
197
198
  return answer(failed('fill', at, 'usage', `${svc.name}'s driver takes no fill step: click the field, then type.`, 'service'));
198
199
  const cwd = svc.cwd ? path.resolve(driving.checkout, svc.cwd) : driving.checkout;
199
- const env = running?.env ?? {
200
- ...fillEnv(svc.env, {}),
201
- ...(svc.isolate && running?.scratch && { [svc.isolate.env]: running.scratch }),
202
- };
200
+ const env = running ? envOf(m, running) : fillEnv(svc.env, {});
203
201
  const ran = await sh(`${svc.drive} ${driveArgv(command).map(quote).join(' ')} --json`, {
204
202
  cwd,
205
203
  env,
@@ -112,7 +112,7 @@ export async function focusedNow(page) {
112
112
  }
113
113
  export async function fieldNow(page, name, role) {
114
114
  try {
115
- const { at } = await theOne(page, name, role);
115
+ const { at } = await theOne(page, name, role, false);
116
116
  const looked = await at.evaluate((el, key) => window[Symbol.for(key)]?.lookAt(el) ?? null, SECRETS);
117
117
  return looked ?? NOTHING_SEEN;
118
118
  }
@@ -4,13 +4,13 @@ import { jsonDifferences, noCounts, setAsideText, textDifferences, } from '../..
4
4
  import { act, contextFor, driveArgv, errorsOf, FIXED_TIME, launch, seenTree, signInBySteps, signInOf, stepCommand, stepOf, stepWords, } from '../../drive/index.js';
5
5
  import { errorText } from '../../errors.js';
6
6
  import { sh, tail } from '../../exec.js';
7
- import { fill, fillEnv, hashOf, readYaml } from '../../manifest/index.js';
7
+ import { fill, hashOf, readYaml } from '../../manifest/index.js';
8
8
  import { scrub, scrubDeep } from '../../people/index.js';
9
9
  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 { handoverSettled, restart, restartable, stateDir } from '../../services/index.js';
13
+ import { envOf, handoverSettled, 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) => {
@@ -96,10 +96,7 @@ async function throughDriver(s, ctx) {
96
96
  const svc = m.services[s.service];
97
97
  const running = ctx.running.find((r) => r.name === svc.name);
98
98
  const cwd = svc.cwd ? path.resolve(ctx.checkout, svc.cwd) : ctx.checkout;
99
- const env = running.env ?? {
100
- ...fillEnv(svc.env, ctx.variables),
101
- ...(svc.isolate && running.scratch && { [svc.isolate.env]: running.scratch }),
102
- };
99
+ const env = envOf(m, running);
103
100
  const drive = async (argv) => {
104
101
  const ran = await sh(`${svc.drive} ${argv.map(quote).join(' ')} --json`, { cwd, env, timeoutMs: 60_000 });
105
102
  try {
@@ -200,18 +200,19 @@ export async function recordAPerson(m, checkout, w) {
200
200
  knownSecrets.add(value);
201
201
  const typedElsewhere = [...typedSecrets]
202
202
  .filter(([field]) => seen.kind !== 'text' || field !== seen.field)
203
- .map(([, value]) => value);
203
+ .flatMap(([, values]) => values);
204
204
  return hiddenInWords(seen.name, [...knownSecrets, ...now, ...typedElsewhere]);
205
205
  };
206
206
  const keepTyped = async (seen, typed) => {
207
207
  const readAfter = typed ? await typed : null;
208
- const value = (seen.kind === 'text' && seen.secretTyped) || readAfter;
209
- if (value && seen.kind === 'text')
210
- typedSecrets.set(seen.field, value);
208
+ const values = [...new Set([seen.kind === 'text' ? seen.secretTyped : null, readAfter])].filter((v) => Boolean(v));
209
+ if (values.length && seen.kind === 'text')
210
+ typedSecrets.set(seen.field, values);
211
211
  if (seen.kind === 'text' || seen.kind === 'unseen' || onlyEdits(seen))
212
212
  return;
213
- for (const v of typedSecrets.values())
214
- knownSecrets.add(v);
213
+ for (const held of typedSecrets.values())
214
+ for (const v of held)
215
+ knownSecrets.add(v);
215
216
  };
216
217
  const beforeNow = () => {
217
218
  const tree = before;
@@ -6,7 +6,7 @@ import { RehearseError, errorText } from '../errors.js';
6
6
  import { broken, runnerOf, } from '../kinds/index.js';
7
7
  import { fill } from '../manifest/index.js';
8
8
  import { signIn } from '../people/index.js';
9
- import { stateDir, bringDown, bringUp, inUse, liveServices, makeRoom, release, standinsFor, } from '../services/index.js';
9
+ import { stateDir, bringDown, bringUp, handoverSettled, inUse, liveServices, makeRoom, release, standinsFor, } from '../services/index.js';
10
10
  import { VERSION } from '../version.js';
11
11
  import { applyWorld, chainOf } from '../worlds/index.js';
12
12
  export { applyWorld };
@@ -248,9 +248,11 @@ async function runGroup(side, world, group, runners, needed, upAlready, rehearse
248
248
  }));
249
249
  }
250
250
  finally {
251
- if (o.keepUp)
251
+ if (o.keepUp) {
252
+ await Promise.all(started.filter((r) => m.services[r.name]?.drive).map(handoverSettled));
252
253
  for (const r of started)
253
254
  release(r);
255
+ }
254
256
  else
255
257
  for (const r of started.toReversed())
256
258
  await bringDown(m, r, o.progress);
@@ -1,2 +1,2 @@
1
1
  export { alive, runningServices, stateDir, type Running } from './registry.ts';
2
- export { bringDown, bringUp, downStartedHere, freePort, handoverSettled, inUse, liveServices, makeRoom, release, restart, restartable, standinsFor, } from './service.ts';
2
+ export { bringDown, bringUp, downStartedHere, envOf, freePort, handoverSettled, inUse, liveServices, makeRoom, release, restart, restartable, standinsFor, } from './service.ts';
@@ -1,2 +1,2 @@
1
1
  export { alive, runningServices, stateDir } from './registry.js';
2
- export { bringDown, bringUp, downStartedHere, freePort, handoverSettled, inUse, liveServices, makeRoom, release, restart, restartable, standinsFor, } from './service.js';
2
+ export { bringDown, bringUp, downStartedHere, envOf, freePort, handoverSettled, inUse, liveServices, makeRoom, release, restart, restartable, standinsFor, } from './service.js';
@@ -10,6 +10,7 @@ export interface Running {
10
10
  standins: string[];
11
11
  up?: string;
12
12
  env?: Record<string, string>;
13
+ vars?: Record<string, string>;
13
14
  world?: string | null;
14
15
  daemon?: boolean;
15
16
  starting?: boolean;
@@ -1,5 +1,5 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
3
3
  import path from 'node:path';
4
4
  export const stateDir = (manifestDir) => path.join(manifestDir, '.rehearse');
5
5
  const registryDir = (manifestDir) => path.join(stateDir(manifestDir), 'services');
@@ -15,8 +15,12 @@ export const alive = (pid) => {
15
15
  }
16
16
  };
17
17
  export function remember(manifestDir, r) {
18
+ const { env: _keptInMemoryOnly, ...onDisk } = r;
19
+ const file = fileOf(manifestDir, r.name, r.checkout);
20
+ const writing = `${file}.${process.pid}.writing`;
18
21
  mkdirSync(registryDir(manifestDir), { recursive: true });
19
- writeFileSync(fileOf(manifestDir, r.name, r.checkout), `${JSON.stringify(r, null, 2)}\n`);
22
+ writeFileSync(writing, `${JSON.stringify(onDisk, null, 2)}\n`);
23
+ renameSync(writing, file);
20
24
  }
21
25
  export function rememberIfStillUp(manifestDir, r) {
22
26
  const file = fileOf(manifestDir, r.name, r.checkout);
@@ -16,6 +16,7 @@ export declare const logOf: (m: Manifest, name: string, checkout: string) => str
16
16
  export declare function liveServices(m: Manifest, checkout?: string | null): Promise<Running[]>;
17
17
  export declare const restartable: (r: Running) => boolean;
18
18
  export declare function handoverSettled(r: Running): Promise<void>;
19
+ export declare function envOf(m: Manifest, r: Running): Record<string, string>;
19
20
  export declare function restart(m: Manifest, r: Running, whileDown?: () => Promise<void>): Promise<string | null>;
20
21
  export declare const release: (r: Running) => boolean;
21
22
  export declare function downStartedHere(): Promise<void>;
@@ -121,9 +121,8 @@ export async function liveServices(m, checkout = null) {
121
121
  continue;
122
122
  }
123
123
  const s = m.services[r.name];
124
- const vars = readyVarsOf(m, r);
125
124
  const answers = s?.ready
126
- ? await readyNow(s.ready, vars, r.env ?? {}, s.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout, '', r.port)
125
+ ? await readyNow(s.ready, varsOf(m, r), envOf(m, r), s.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout, '', r.port)
127
126
  : r.port !== null
128
127
  ? await portTaken(r.port)
129
128
  : false;
@@ -140,28 +139,57 @@ export async function handoverSettled(r) {
140
139
  const still = upsStillExiting.get(r);
141
140
  if (!still)
142
141
  return;
143
- let timer;
144
- const windowCloses = new Promise((resolve) => (timer = setTimeout(resolve, still.until - Date.now())));
145
- await Promise.race([still.exiting, windowCloses]);
146
- clearTimeout(timer);
142
+ const left = still.until - Date.now();
143
+ if (left > 0) {
144
+ let timer;
145
+ const windowCloses = new Promise((resolve) => (timer = setTimeout(resolve, left)));
146
+ await Promise.race([still.exiting, windowCloses]);
147
+ clearTimeout(timer);
148
+ }
147
149
  upsStillExiting.delete(r);
148
150
  }
149
- function readyVarsOf(m, r) {
150
- return {
151
+ function varsOf(m, r) {
152
+ return (r.vars ?? {
151
153
  checkout: r.checkout,
152
154
  manifest: m.dir,
153
155
  ...(r.scratch && { scratch: r.scratch }),
154
156
  ...(r.world && { world: r.world }),
155
157
  ...(r.port !== null && { port: String(r.port) }),
156
158
  ...(r.url && { url: r.url }),
159
+ });
160
+ }
161
+ function serviceEnv(s, standins, vars, scratch, port) {
162
+ return {
163
+ ...fillEnv(s.env, vars),
164
+ ...Object.assign({}, ...standins.map(({ standin, real }) => fillEnv(real ? standin.real.env : standin.env, vars))),
165
+ ...(s.isolate && scratch && { [s.isolate.env]: scratch }),
166
+ ...(port !== null && { PORT: String(port) }),
167
+ ...(scratch && { REHEARSE_SCRATCH: scratch }),
157
168
  };
158
169
  }
170
+ const REAL = ':real';
171
+ export function envOf(m, r) {
172
+ if (r.env)
173
+ return r.env;
174
+ const s = m.services[r.name];
175
+ if (!s)
176
+ return {
177
+ ...(r.port !== null && { PORT: String(r.port) }),
178
+ ...(r.scratch && { REHEARSE_SCRATCH: r.scratch }),
179
+ };
180
+ const standins = r.standins.flatMap((named) => {
181
+ const real = named.endsWith(REAL);
182
+ const standin = m.standins[real ? named.slice(0, -REAL.length) : named];
183
+ return standin && (!real || standin.real) ? [{ standin, real }] : [];
184
+ });
185
+ return serviceEnv(s, standins, varsOf(m, r), r.scratch, r.port);
186
+ }
159
187
  export async function restart(m, r, whileDown = async () => undefined) {
160
188
  const s = m.services[r.name];
161
189
  if (!s || !restartable(r))
162
190
  return null;
163
191
  const cwd = s.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout;
164
- const env = r.env ?? {};
192
+ const env = envOf(m, r);
165
193
  await sh(r.down, { cwd, env, timeoutMs: 60_000 });
166
194
  await whileDown();
167
195
  const up = await sh(r.up, { cwd, env, timeoutMs: s.timeoutMs });
@@ -169,7 +197,7 @@ export async function restart(m, r, whileDown = async () => undefined) {
169
197
  writeFileSync(upSaidThisTime, `${up.stdout}${up.stderr}`);
170
198
  if (up.code !== 0)
171
199
  return `${s.name} didn't come up again (exit ${up.code}): ${tail(up.stderr || up.stdout)}`;
172
- const vars = readyVarsOf(m, r);
200
+ const vars = varsOf(m, r);
173
201
  const begun = Date.now();
174
202
  while (!(await readyNow(s.ready, vars, env, cwd, upSaidThisTime, r.port))) {
175
203
  if (Date.now() - begun > s.timeoutMs)
@@ -198,7 +226,7 @@ export async function bringUp(m, s, checkout, options) {
198
226
  if (await readyNow(s.ready, {
199
227
  ...(already.url && { url: already.url }),
200
228
  ...(already.port !== null && { port: String(already.port) }),
201
- }, already.env ?? {}, s.cwd ? path.resolve(checkout, s.cwd) : checkout, '', already.port))
229
+ }, envOf(m, already), s.cwd ? path.resolve(checkout, s.cwd) : checkout, '', already.port))
202
230
  return already;
203
231
  forget(m.dir, already.name, already.checkout);
204
232
  }
@@ -227,13 +255,7 @@ export async function bringUp(m, s, checkout, options) {
227
255
  const url = s.url === null ? null : fill(s.url, vars);
228
256
  if (url !== null)
229
257
  vars.url = url;
230
- const env = {
231
- ...fillEnv(s.env, vars),
232
- ...Object.assign({}, ...standins.map(({ standin, real }) => fillEnv(real ? standin.real.env : standin.env, vars))),
233
- ...(s.isolate && { [s.isolate.env]: scratch }),
234
- ...(port !== null && { PORT: String(port) }),
235
- REHEARSE_SCRATCH: scratch,
236
- };
258
+ const env = serviceEnv(s, standins, vars, scratch, port);
237
259
  if (stoppingWhatStartedHere) {
238
260
  rmSync(scratch, { recursive: true, force: true });
239
261
  throw new RehearseError('service', `Rehearse is stopping, so it doesn't bring ${s.name} up.`);
@@ -273,6 +295,7 @@ export async function bringUp(m, s, checkout, options) {
273
295
  standins: standins.map(({ standin, real }) => (real ? `${standin.name}:real` : standin.name)),
274
296
  up: command,
275
297
  env,
298
+ vars,
276
299
  world: options.world,
277
300
  starting: true,
278
301
  };
@@ -317,7 +340,7 @@ export async function bringDown(m, r, progress) {
317
340
  await sh(r.down, {
318
341
  cwd,
319
342
  timeoutMs: 30_000,
320
- env: r.env ?? (r.scratch && s?.isolate ? { [s.isolate.env]: r.scratch } : {}),
343
+ env: envOf(m, r),
321
344
  });
322
345
  }
323
346
  killGroup(r.pid, 'SIGTERM');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kurvball/rehearse",
3
- "version": "1.13.1",
3
+ "version": "1.13.3",
4
4
  "description": "Rehearse a change before it ships: bring the product up, sign in as named people, start from named worlds, and compare any scenario on the base and on the head.",
5
5
  "homepage": "https://rehearse.kurvball.ca/",
6
6
  "repository": {