@vzn/vx 0.0.26 → 0.0.27

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vzn/vx",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
4
4
  "description": "An open, extensible monorepo task runner.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,10 +17,10 @@
17
17
  "node": ">=18"
18
18
  },
19
19
  "optionalDependencies": {
20
- "@vzn/vx-linux-x64": "0.0.26",
21
- "@vzn/vx-linux-arm64": "0.0.26",
22
- "@vzn/vx-darwin-x64": "0.0.26",
23
- "@vzn/vx-darwin-arm64": "0.0.26"
20
+ "@vzn/vx-linux-x64": "0.0.27",
21
+ "@vzn/vx-linux-arm64": "0.0.27",
22
+ "@vzn/vx-darwin-x64": "0.0.27",
23
+ "@vzn/vx-darwin-arm64": "0.0.27"
24
24
  },
25
25
  "dependencies": {
26
26
  "@anthropic-ai/sandbox-runtime": "^0.0.76"
@@ -396,9 +396,10 @@ export async function resolveExecutors(
396
396
  * End-of-run plugin lifecycle: each plugin's optional `teardown()`, in
397
397
  * declaration order. Crash-isolated — a throwing teardown is logged and
398
398
  * skipped, never propagated — and each call is time-bounded by
399
- * {@link teardownTimeoutMs}. Runs on the normal completion path only; the
400
- * finally-path disposers just unsubscribe. (Telemetry sinks flush before
401
- * this, in telemetry-host.ts, with the same reporting rule.)
399
+ * {@link teardownTimeoutMs}. Runs on every exit of a run once its plugins'
400
+ * factories ran, and of a plan (item 1021); the finally-path disposers
401
+ * just unsubscribe. (Telemetry sinks flush before this, in
402
+ * telemetry-host.ts, with the same reporting rule.)
402
403
  *
403
404
  * The bound is PER CALL and the calls are sequential, so the worst case
404
405
  * composes: measured at the 3s default, 1/2/3 simultaneously-hung plugins
@@ -442,6 +442,20 @@ export interface InstallPluginsArgs {
442
442
  warn?: (message: string) => void
443
443
  }
444
444
 
445
+ /**
446
+ * A plugin's `setup` threw. It names the plugin so the run can tear down
447
+ * every other one: their capability factories and setups ran, and the
448
+ * one that failed set up nothing to release.
449
+ */
450
+ export class PluginSetupError extends UserError {
451
+ constructor(
452
+ readonly plugin: Plugin,
453
+ message: string,
454
+ ) {
455
+ super(message)
456
+ }
457
+ }
458
+
445
459
  /**
446
460
  * Install every plugin's `setup` hook against a shared bus + context.
447
461
  * Synchronous loop; setup() promises are awaited in order so a plugin's
@@ -534,7 +548,8 @@ export async function installPlugins(args: InstallPluginsArgs): Promise<() => vo
534
548
  try {
535
549
  await plugin.setup(ctx)
536
550
  } catch (err) {
537
- throw new UserError(
551
+ throw new PluginSetupError(
552
+ plugin,
538
553
  `plugin '${plugin.name}' failed to load: ${err instanceof Error ? err.message : String(err)}`,
539
554
  )
540
555
  }
@@ -39,7 +39,7 @@ import { admitTasks, taintTracker } from './admission.js'
39
39
  import { excludedTaint } from './excluded-keys.js'
40
40
  import { keyUpstream } from './upstream.js'
41
41
  import { busLogger, createEventBus, terminalSubscriber, type EventBus } from './events.js'
42
- import { installPlugins } from './plugin.js'
42
+ import { installPlugins, PluginSetupError } from './plugin.js'
43
43
  import { buildAdmission, resolveExecutors, teardownPlugins } from './plugin-host.js'
44
44
  import { subscribeTelemetry, type TelemetryHandle } from './telemetry-host.js'
45
45
  import { assembleRunSummary, isPassStatus } from './telemetry.js'
@@ -217,6 +217,19 @@ async function runOnBus(
217
217
 
218
218
  const prepared = await prepareRun(options, log)
219
219
  mark('plugin stages')
220
+ // Every exit from here tears the plugins down: their capability
221
+ // factories ran in prepareRun, and teardown is what releases what those
222
+ // opened (a client, a channel). Until item 1021 only the normal end did,
223
+ // so an early return, a refused setup or a throw left them held — once
224
+ // per cycle under `vx watch`. A plugin whose own setup threw set up
225
+ // nothing, and is the one left out.
226
+ let tornDown = false
227
+ const teardown = async (except?: unknown): Promise<void> => {
228
+ if (tornDown) return
229
+ tornDown = true
230
+ const plugins = prepared.plugins.filter((p) => p !== except)
231
+ await teardownPlugins(plugins, (m) => log.status(m))
232
+ }
220
233
  // A task whose command re-enters `vx run` in the workspace running it
221
234
  // is refused. When the inner run reaches this task again it forks a run
222
235
  // per run until the machine gives out; when it does not (`ci` shelling
@@ -226,6 +239,7 @@ async function runOnBus(
226
239
  // name the task; the check is on the root so both shapes are caught.
227
240
  const outerRoot = process.env[VX_RUN_WORKSPACE_ENV]
228
241
  if (outerRoot !== undefined && path.resolve(outerRoot) === path.resolve(prepared.workspaceRoot)) {
242
+ await teardown()
229
243
  prepared.cache.close()
230
244
  throw new UserError(
231
245
  `task ${process.env[VX_RUN_TASK_ENV] ?? '<unknown>'} runs \`vx run\` inside its own workspace: a nested run is invisible to the outer graph (its tasks escape the schedule, the concurrency budget and the cache key) and a loop back to this task forks without bound. Declare what it needs with dependsOn instead.`,
@@ -241,6 +255,7 @@ async function runOnBus(
241
255
  log.status(
242
256
  `No projects declare task(s): ${prepared.unresolvedTasks.join(', ')}.${didYouMean(prepared.unresolvedTasks, prepared.projects)}${await initHint(prepared)}`,
243
257
  )
258
+ await teardown()
244
259
  prepared.cache.close()
245
260
  return { ok: false, outcomes: [] }
246
261
  }
@@ -254,6 +269,7 @@ async function runOnBus(
254
269
  ? `No projects declare task(s): ${options.tasks.join(', ')}.${await initHint(prepared)}`
255
270
  : 'No tasks to run.'
256
271
  log.status(msg)
272
+ await teardown()
257
273
  prepared.cache.close()
258
274
  return { ok: false, outcomes: [] }
259
275
  }
@@ -273,9 +289,21 @@ async function runOnBus(
273
289
  })
274
290
  } catch (err) {
275
291
  disposePlugins?.()
292
+ await teardown(err instanceof PluginSetupError ? err.plugin : undefined)
276
293
  prepared.cache.close()
277
294
  throw err
278
295
  }
296
+ // Until the run's own try below, a throw leaves through here.
297
+ const abandoning = async <T>(step: () => T | Promise<T>): Promise<T> => {
298
+ try {
299
+ return await step()
300
+ } catch (err) {
301
+ disposePlugins?.()
302
+ await teardown()
303
+ prepared.cache.close()
304
+ throw err
305
+ }
306
+ }
279
307
  const {
280
308
  workspaceRoot,
281
309
  workspaceConfig,
@@ -293,24 +321,19 @@ async function runOnBus(
293
321
 
294
322
  // Resolved ONCE per run, in declaration order, the local executor last.
295
323
  // A broken factory aborts here, before any task starts.
296
- let executors: readonly TaskExecutor[]
297
- try {
298
- executors = await resolveExecutors(prepared.plugins, {
324
+ const executors: readonly TaskExecutor[] = await abandoning(() =>
325
+ resolveExecutors(prepared.plugins, {
299
326
  workspaceRoot: prepared.workspaceRoot,
300
327
  cacheDir: prepared.cacheDir,
301
328
  warn: (m: string) => log.status(m),
302
329
  concurrency,
303
- })
304
- } catch (err) {
305
- disposePlugins?.()
306
- prepared.cache.close()
307
- throw err
308
- }
330
+ }),
331
+ )
309
332
  // Placement: decided ONCE per task, before scheduling, so the scheduler
310
333
  // can admit a remote-pooled task against its pool instead of a local
311
334
  // worker slot. Group tasks run nothing; persistent tasks never reach an
312
335
  // executor (local by construction) — both stay off the map.
313
- const placements = placeTasks(nodes, executors)
336
+ const placements = await abandoning(() => placeTasks(nodes, executors))
314
337
  // A `remote: 'only'` task nobody takes succeeds WITHOUT running. That is
315
338
  // deliberate — on a machine with no remote pool the ambient state already
316
339
  // is what the task would have produced — but it must not be SILENT: a task
@@ -342,7 +365,9 @@ async function runOnBus(
342
365
  }
343
366
  // The SAME set is handed back every time, narrowed in place: a consumer
344
367
  // reading it later sees the truth rather than a stale snapshot.
345
- for (const [executor, remaining] of demandOf) executor.demand!(remaining)
368
+ await abandoning(() => {
369
+ for (const [executor, remaining] of demandOf) executor.demand!(remaining)
370
+ })
346
371
  const narrowDemand =
347
372
  demandOf.size === 0
348
373
  ? (): void => {}
@@ -457,10 +482,12 @@ async function runOnBus(
457
482
  }
458
483
  // Taken after the early exits above (nothing they do touches a tree) and
459
484
  // before the schedule: from here on tasks clean, restore and write.
460
- const releaseRunLock = await acquireRunLock(prepared.workspaceRoot, {
461
- log: (m) => log.status(m),
462
- signal: stopRun.signal,
463
- })
485
+ const releaseRunLock = await abandoning(() =>
486
+ acquireRunLock(prepared.workspaceRoot, {
487
+ log: (m) => log.status(m),
488
+ signal: stopRun.signal,
489
+ }),
490
+ )
464
491
  try {
465
492
  // One run-id per `vx run` invocation. Every task in the resulting
466
493
  // graph carries it so analytics queries can group by invocation.
@@ -900,8 +927,8 @@ async function runOnBus(
900
927
  }
901
928
  // End-of-run plugin lifecycle: each plugin's teardown(). Crash-isolated
902
929
  // + time-bounded inside teardownPlugins, so a faulty plugin can neither
903
- // fail nor hang the run. Normal completion path only — the finally
904
- // below just unsubscribes.
930
+ // fail nor hang the run. Here on the normal path, after the drains
931
+ // below; every other exit takes it through `teardown` too.
905
932
  // Drain any still-in-flight background prefetches before closing the
906
933
  // cache handle — a prefetch ingesting into a closed SQLite DB would
907
934
  // throw. Tasks that resolved as local hits never awaited their
@@ -939,7 +966,7 @@ async function runOnBus(
939
966
  // Not on a stopped run: one stopped while it waited on another run's
940
967
  // lock never held it, and its prune evicted under that run (item 858).
941
968
  if (!stopRun.signal.aborted) await applyCacheRetention(prepared, log)
942
- await teardownPlugins(prepared.plugins, (m) => log.status(m))
969
+ await teardown()
943
970
  await closeCache()
944
971
  mark('close')
945
972
  printTimings()
@@ -1019,6 +1046,8 @@ async function runOnBus(
1019
1046
  // released here. Idempotent; safe even if installPlugins threw.
1020
1047
  disposePlugins?.()
1021
1048
  telemetry?.dispose()
1049
+ // No-op after the normal path's teardown; on a throw, the only one.
1050
+ await teardown()
1022
1051
  // No-op after the normal path's close. On a throw this is the only
1023
1052
  // close there is, and it must not itself throw — that would replace
1024
1053
  // the run's real error with a teardown one. Background uploads are
@@ -1123,6 +1152,9 @@ export async function planRun(options: RunOptions): Promise<RunPlan> {
1123
1152
  mark('plan')
1124
1153
  return planned
1125
1154
  } finally {
1155
+ // The plan called the cache and executor factories; teardown releases
1156
+ // what they opened, as a run's end does (item 1021).
1157
+ await teardownPlugins(prepared.plugins, (m) => log.status(m))
1126
1158
  prepared.cache.close()
1127
1159
  // The same table a run prints: a dry run is how the prepare stages
1128
1160
  // (discovery, config load, the git enumeration) get profiled on a