@smoothbricks/nx-plugin 0.7.6 → 0.7.7

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 (26) hide show
  1. package/dist/.build-inputs.json +7 -7
  2. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  3. package/dist/executors/bounded-exec/executor.js +17 -17
  4. package/dist/executors/bounded-exec/host-memory.d.ts +7 -0
  5. package/dist/executors/bounded-exec/host-memory.d.ts.map +1 -1
  6. package/dist/executors/bounded-exec/host-memory.darwin.d.ts.map +1 -1
  7. package/dist/executors/bounded-exec/host-memory.darwin.js +76 -1
  8. package/dist/executors/bounded-exec/host-memory.linux.d.ts.map +1 -1
  9. package/dist/executors/bounded-exec/host-memory.linux.js +34 -1
  10. package/dist/executors/bounded-exec/leaf-peak.d.ts +5 -4
  11. package/dist/executors/bounded-exec/leaf-peak.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/leaf-peak.js +5 -4
  13. package/dist/executors/bounded-exec/mbx-reservations.d.ts +22 -0
  14. package/dist/executors/bounded-exec/mbx-reservations.d.ts.map +1 -1
  15. package/dist/executors/bounded-exec/mbx-reservations.js +22 -0
  16. package/dist/executors/bounded-exec/peak-waiter.js +27 -1
  17. package/managed/templates/github/actions/setup-devenv/action.yml +31 -2
  18. package/package.json +2 -2
  19. package/src/executors/bounded-exec/executor.ts +18 -18
  20. package/src/executors/bounded-exec/host-memory.darwin.ts +104 -1
  21. package/src/executors/bounded-exec/host-memory.linux.ts +33 -1
  22. package/src/executors/bounded-exec/host-memory.ts +7 -0
  23. package/src/executors/bounded-exec/leaf-peak.ts +5 -4
  24. package/src/executors/bounded-exec/mbx-reservations.ts +30 -0
  25. package/src/executors/bounded-exec/peak-waiter.ts +37 -4
  26. package/test-timings.json +2 -0
@@ -38,21 +38,21 @@
38
38
  "packages/nx-plugin/src/executors/bounded-exec/cpu-pool-thread.ts": "bc7d113a044bb830e4db4c373c2f366fadfbb212353e34ef9a5e7dccf876aee6",
39
39
  "packages/nx-plugin/src/executors/bounded-exec/cpu-pool.ts": "0f2085296486ebfc63b356fb92c4ec77670de5ac9877bbaf2772ea3aaabbf9f9",
40
40
  "packages/nx-plugin/src/executors/bounded-exec/disk-lease.ts": "1a3f6a6fa590d9a2550490bb6906dc91b6c05fbef235b025eaf257eb5310e92d",
41
- "packages/nx-plugin/src/executors/bounded-exec/executor.ts": "63ea2da25f290b458c2ed29bb83c10aebd1236e4cc4b0f9a5da78e8f23de4614",
41
+ "packages/nx-plugin/src/executors/bounded-exec/executor.ts": "49c29558c8b4f3db21a807d6c2e453ca5805ffd8b41f8f79d3837fde33a92396",
42
42
  "packages/nx-plugin/src/executors/bounded-exec/gateway-lease.ts": "f3f73bd92db30e4857f98fb8bd2b9a75b2a5a2a318f094e075ed803c1f9a0117",
43
43
  "packages/nx-plugin/src/executors/bounded-exec/hang-diagnostics.ts": "bcaa9e52384f6c481a9cc2835ba6e8ffe167afa3cb51623fdf8dc3493876f335",
44
- "packages/nx-plugin/src/executors/bounded-exec/host-memory.darwin.ts": "a7bfc682d004d073f59abfc1b8637d00a545b26744b9605e942a2de7aef651a9",
45
- "packages/nx-plugin/src/executors/bounded-exec/host-memory.linux.ts": "f4762c05dcc36e9d2a2059d0723e99e9810be1f647493178626ff070b6b00e13",
46
- "packages/nx-plugin/src/executors/bounded-exec/host-memory.ts": "d0ee70e3e406e3d48a573875fa8dbc60dc7349d31ddd6888b49e1207eed6b744",
47
- "packages/nx-plugin/src/executors/bounded-exec/leaf-peak.ts": "3d4934d62f7c37e57ce050be45fefb2bc9f2ca3049bef33670c44fc572324f6d",
44
+ "packages/nx-plugin/src/executors/bounded-exec/host-memory.darwin.ts": "c27868c1240ec9c782f80c7848f5eee1bc59146e3a56a6c8c0fc05bcf82b2931",
45
+ "packages/nx-plugin/src/executors/bounded-exec/host-memory.linux.ts": "d473dc8475066d4914c419b6b803b6937ebb2577348f0dda8f9ed9def60e8584",
46
+ "packages/nx-plugin/src/executors/bounded-exec/host-memory.ts": "cc7e94023adad2c3645c7a4ba18d07545426ae8d02840a2cf8117494d0915234",
47
+ "packages/nx-plugin/src/executors/bounded-exec/leaf-peak.ts": "0b1b94992b2cbb9e3759785424ceb202667053c5571edf3a79cdd0b206e54a1d",
48
48
  "packages/nx-plugin/src/executors/bounded-exec/lease-wait.ts": "59d8ac94d0b8b75858971c5bb28ee421fca4f7685577274fe8bbaf0c01ba462a",
49
49
  "packages/nx-plugin/src/executors/bounded-exec/lock-wait.ts": "b14a23b49759a777f4b3338ced003adaf76837c2c38462458f76a7fef1b95d9e",
50
50
  "packages/nx-plugin/src/executors/bounded-exec/mbx-pool.ts": "4ef48c1887c1fb29000cdc581686332db6fd503ee07ccdb5f0241fb033249097",
51
51
  "packages/nx-plugin/src/executors/bounded-exec/mbx-queue.ts": "ac309e28e5ee78fa08d628edb00e33532514f7572166770799ae86d468c12b4e",
52
- "packages/nx-plugin/src/executors/bounded-exec/mbx-reservations.ts": "65087d9fdb3f7e788a127d0a7261701b7cb18b0b520ca5cef4b8f7b6fc33d3c5",
52
+ "packages/nx-plugin/src/executors/bounded-exec/mbx-reservations.ts": "1d51ef403f1ea3606d570370265d21421add7d5ca48d74480e9cde12793d0786",
53
53
  "packages/nx-plugin/src/executors/bounded-exec/no-space.ts": "ec032968d89f9e853796012705a06c06cfc4da54bf6e6f10a526f98daca3d077",
54
54
  "packages/nx-plugin/src/executors/bounded-exec/owned-subtree.ts": "cb5edf2b5fe2c1b5b961f8c0ff16ec7bfae3c22ad75dfd671b74f1a4a865863f",
55
- "packages/nx-plugin/src/executors/bounded-exec/peak-waiter.ts": "27d51f75f182125b555643cdc14463d41139abd49cba59dccf8a20fcae136c56",
55
+ "packages/nx-plugin/src/executors/bounded-exec/peak-waiter.ts": "3d9a60f8e61d160cf4151746c160f836954f57aca896127a5ef9df4d09c26180",
56
56
  "packages/nx-plugin/src/executors/bounded-exec/pool-admission.ts": "384819f66b486a4075685d9a5c240011a342966eeb5c39ecafe1656529f16c21",
57
57
  "packages/nx-plugin/src/executors/bounded-exec/progress.ts": "ed926f7b4991ebf7a7c8876ab0ea00295934d058c774e3f811963a85d209979b",
58
58
  "packages/nx-plugin/src/executors/bounded-exec/ram-attach.ts": "865eb5c277d9ab4b258013030938aeee762b731a7298fa8ad3c5eba3026df77a",
@@ -1 +1 @@
1
- {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/executor.ts"],"names":[],"mappings":"AA0BA,OAAO,EAIL,KAAK,gBAAgB,EAItB,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,sBAAsB,EAAmB,MAAM,uBAAuB,CAAC;AAahF,OAAO,EAA4C,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAG5F,OAAO,EAOL,KAAK,YAAY,EAClB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EASL,aAAa,EAEd,MAAM,eAAe,CAAC;AAIvB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,OAAO,EAGL,KAAK,eAAe,EAgBrB,MAAM,cAAc,CAAC;AAuBtB,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,wFAAwF;IACxF,MAAM,CAAC,EAAE;QAAE,IAAI,EAAE,eAAe,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;IACtD,uFAAuF;IACvF,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CAClC;AAED,qEAAqE;AACrE,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,SAAS,CAAC,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE;YAAE,IAAI,CAAC,EAAE,MAAM,CAAA;SAAE,CAA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,EAAE,kBAAkB,EAC3B,OAAO,EAAE,kBAAkB,EAC3B,MAAM,EAAE,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,KAAK,UAAU,GAAG,IAAI,GACzD,OAAO,CAAC,iBAAiB,CAAC,CAsB5B;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,aAAa,GAAG,kBAAkB,CAiBxE;AAED;;;;;;;;GAQG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,kBAAkB,EAC3B,OAAO,EAAE,kBAAkB,EAC3B,MAAM,EAAE,iBAAiB,EACzB,UAAU,EAAE,UAAU,GAAG,IAAI,EAC7B,SAAS,GAAE,gBAAgB,GAAG,IAAW,EACzC,kBAAkB,GAAE,OAAO,sBAA+C,EAC1E,KAAK,GAAE,YAAY,GAAG,IAAW,EACjC,SAAS,CAAC,EAAE,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC,CAAC,CAAC,GACjD,OAAO,CAAC,iBAAiB,CAAC,CA4pC5B;AAED,wBAAgB,uBAAuB,IAAI,iBAAiB,CAM3D"}
@@ -1,7 +1,7 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { accessSync, constants } from 'node:fs';
3
3
  import { mkdtemp, rm } from 'node:fs/promises';
4
- import { availableParallelism, userInfo } from 'node:os';
4
+ import { availableParallelism, constants as osConstants, userInfo } from 'node:os';
5
5
  import { delimiter, isAbsolute, join, resolve as resolvePath } from 'node:path';
6
6
  import { createInterface } from 'node:readline';
7
7
  import { Duplex, Readable } from 'node:stream';
@@ -14,7 +14,7 @@ import { captureHangDiagnostics, HANG_CAPTURE_MS } from './hang-diagnostics.js';
14
14
  import { PEAK_FD, peakWaiter, readWaiterReport, underWaiter } from './leaf-peak.js';
15
15
  import { askedCores, MBX_GRANT_FD, mbxIdleGated, mbxLeaf, mbxPool, withCountedReservation, withMbxLeaf, } from './mbx-pool.js';
16
16
  import { MBX_QUEUE_POLL_MS, snapshotMbxQueue } from './mbx-queue.js';
17
- import { RESERVATIONS_ENV, reserveCores } from './mbx-reservations.js';
17
+ import { describeUnserved, RESERVATIONS_ENV, reserveCores, runEndpoint } from './mbx-reservations.js';
18
18
  import { createNoSpaceWatch } from './no-space.js';
19
19
  import { COWSHED_EXECUTABLE_ENV, cowshedSubtreeOwner, HANDLE_FD, readAdmission, SUPERVISOR_SOCKET_ENV, } from './owned-subtree.js';
20
20
  import { createRunProgress, withProgressReport, withTestResultReport } from './progress.js';
@@ -26,11 +26,6 @@ import { createTestResultWriter } from './test-result-writer.js';
26
26
  import { BUN_TEST, clearTaskDirectory, describeAdmissionQueue, describeNoSpace, JUNIT_FILE, JUNIT_REPORT_ENV, judgeOperationalFailure, judgeRun, readJunitReport, readLastPeak, taskDirectory, withJunitReport, writeRecord, } from './verdict.js';
27
27
  const DEFAULT_KILL_AFTER_MS = 10_000;
28
28
  const REAP_AFTER_FORCE_KILL_MS = 2_000;
29
- const EXIT_CODE_BY_SIGNAL = {
30
- SIGHUP: 129,
31
- SIGINT: 130,
32
- SIGTERM: 143,
33
- };
34
29
  /** The pool descriptor: only a grant may give it, so neither the target's nor the inherited environment passes it on. */
35
30
  const POOL_ENV = [CPU_TOKENS_ENV, CPU_POOL_ENV];
36
31
  /**
@@ -516,11 +511,13 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
516
511
  return refuse(`cowshed: cpu-pool parent pool refused: ${parentRefusal}; refusing to run the command\n`);
517
512
  }
518
513
  // A reserved leaf takes its cores from its Nx run's mbx reservation endpoint (mbx-reservations.ts). A run that serves
519
- // none said why in its own output; the leaf does not run beside the pool.
520
- const reserved = leaf?.kind === 'reserve' ? { leaf, endpoint: process.env[RESERVATIONS_ENV] ?? '' } : null;
521
- if (reserved !== null && reserved.endpoint === '') {
522
- return refuse(`cowshed: a reserved leaf takes its cores from its Nx run's mbx reservation endpoint, and ${RESERVATIONS_ENV} names none (the run's preTasksExecution serves it); refusing to run the command\n`);
514
+ // none said why in its own output, which a gate's failures-only output never shows, so the refusal says it too; the
515
+ // leaf does not run beside the pool.
516
+ const endpoint = leaf?.kind === 'reserve' ? runEndpoint(process.env) : null;
517
+ if (endpoint !== null && endpoint.kind !== 'served') {
518
+ return refuse(`cowshed: a reserved leaf takes its cores from its Nx run's mbx reservation endpoint, and ${RESERVATIONS_ENV} names none: ${describeUnserved(endpoint)}; refusing to run the command\n`);
523
519
  }
520
+ const reserved = leaf?.kind === 'reserve' && endpoint?.kind === 'served' ? { leaf, endpoint: endpoint.socket } : null;
524
521
  // A granted run serves a sub-pool of its own grant, so its descendants draw from that and never from above it.
525
522
  // A pool that cannot be set up refuses the run: no granted command runs beside its grant.
526
523
  if (grant !== null) {
@@ -1202,9 +1199,14 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
1202
1199
  await releaseCpu();
1203
1200
  const code = exitCode ?? signalToExitCode(exitSignal);
1204
1201
  if (code !== 0 && state.expiry === null) {
1202
+ // No bound of this executor ended it, so a signal it died of came from outside (the kernel's OOM killer sends
1203
+ // SIGKILL): the signal is named, never folded into a bare status.
1204
+ const ended = exitCode === null && exitSignal !== null
1205
+ ? `was killed by ${exitSignal} (status ${code})`
1206
+ : `exited with status ${code}`;
1205
1207
  appendStderr(fixture !== null && !mainStarted
1206
- ? `Fixture command exited with status ${code}: ${segment ?? fixture.command}\n`
1207
- : `Command exited with status ${code}: ${command}\n`);
1208
+ ? `Fixture command ${ended}: ${segment ?? fixture.command}\n`
1209
+ : `Command ${ended}: ${command}\n`);
1208
1210
  }
1209
1211
  if (state.expiry !== null && !state.forceKillNeeded) {
1210
1212
  appendStderr(`Timed out command exited after graceful termination: ${command}\n`);
@@ -1292,11 +1294,9 @@ function targetEnv(env, say) {
1292
1294
  }
1293
1295
  return kept;
1294
1296
  }
1297
+ /** The status a shell gives a command a signal ended: 128 plus the signal's number (SIGKILL 137, SIGTERM 143). */
1295
1298
  function signalToExitCode(signal) {
1296
- if (!signal) {
1297
- return 1;
1298
- }
1299
- return EXIT_CODE_BY_SIGNAL[signal] ?? 1;
1299
+ return signal === null ? 1 : 128 + osConstants.signals[signal];
1300
1300
  }
1301
1301
  /** Resolves when `promise` settles or `ms` passes, whichever is first, leaving no timer behind. */
1302
1302
  async function settledWithin(promise, ms) {
@@ -11,6 +11,13 @@ export interface HostMemory {
11
11
  * it: the exited leaf's `ru_maxrss`.
12
12
  */
13
13
  peakBytes(maxRss: number, bunVersion: string): number;
14
+ /**
15
+ * What the process tree rooted at `root` holds resident at this instant: the sum over `root` and every descendant the
16
+ * kernel links to it, each process's footprint where the platform keeps one (macOS `ri_phys_footprint`, which counts
17
+ * its compressed pages) and its resident set where it does not (Linux `VmRSS`). A process that exits between the
18
+ * listing and its read adds nothing, and a root that has exited holds nothing.
19
+ */
20
+ treeResidentBytes(root: number): Promise<number>;
14
21
  }
15
22
  /** The run's memory in bytes, and where it was read: the file or sysctl that holds it. */
16
23
  export interface RunMemory {
@@ -1 +1 @@
1
- {"version":3,"file":"host-memory.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/host-memory.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,mEAAmE;IACnE,aAAa,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC3C;;;OAGG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;CACvD;AAED,0FAA0F;AAC1F,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,4FAA4F;AAC5F,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,SAAS,EAAE,iBAAiB,CAAC;IACtC;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,WAAW,GAAG,WAAW,GAAG,sBAAsB,CAAC;IACjF,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,E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CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,MAAM,gBAAgB,GACxB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAA;CAAE,GACjD;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAA;CAAE,CAAC;AAE3D,sFAAsF;AACtF,qBAAa,mBAAoB,SAAQ,KAAK;IAChC,QAAQ,CAAC,KAAK,EAAE,cAAc;IAA1C,YAAqB,KAAK,EAAE,cAAc,EAKzC;CACF;AAID,kGAAkG;AAClG,eAAO,MAAM,WAAW,EAAE,UAAU,GAAG,IAA0C,CAAC;AAElF,mEAAmE;AACnE,wBAAgB,aAAa,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAazD"}
@@ -1 +1 @@
1
- {"version":3,"file":"host-memory.darwin.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/host-memory.darwin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,kBAAkB,CAAC;AAyCrE,eAAO,MAAM,YAAY,EAAE,UAA6D,CAAC"}
1
+ {"version":3,"file":"host-memory.darwin.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/host-memory.darwin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,kBAAkB,CAAC;AAgJrE,eAAO,MAAM,YAAY,EAAE,UAAgF,CAAC"}
@@ -37,4 +37,79 @@ async function readRunMemory() {
37
37
  }
38
38
  return { ok: true, memory: { bytes, source: HW_MEMSIZE } };
39
39
  }
40
- export const darwinMemory = { readRunMemory, peakBytes: (maxRss) => maxRss };
40
+ /**
41
+ * Bun's FFI, as the peak waiter reaches it. Node, which runs the executor, loads this module too and has no `bun:ffi`;
42
+ * only the waiter, on Bun, reads a tree's footprint, so the FFI loads on its first read, by a specifier the compiler
43
+ * does not resolve against Node's types.
44
+ */
45
+ const BUN_FFI = 'bun:ffi';
46
+ const LIBPROC = '/usr/lib/libproc.dylib';
47
+ /** `proc_pid_rusage`'s oldest flavor, whose fields every later one starts with. */
48
+ const RUSAGE_INFO_V0 = 0;
49
+ /** `rusage_info_v0`: a 16-byte uuid, then ten u64 counters; `ri_phys_footprint` is the eighth, u64 index 9. */
50
+ const RUSAGE_INFO_V0_WORDS = 12;
51
+ const PHYS_FOOTPRINT = 9;
52
+ function isBunFfi(module) {
53
+ return (typeof module === 'object' &&
54
+ module !== null &&
55
+ 'dlopen' in module &&
56
+ typeof module.dlopen === 'function' &&
57
+ 'ptr' in module &&
58
+ typeof module.ptr === 'function');
59
+ }
60
+ function isLibprocSymbols(symbols) {
61
+ return (typeof symbols === 'object' &&
62
+ symbols !== null &&
63
+ 'proc_listchildpids' in symbols &&
64
+ typeof symbols.proc_listchildpids === 'function' &&
65
+ 'proc_pid_rusage' in symbols &&
66
+ typeof symbols.proc_pid_rusage === 'function');
67
+ }
68
+ let footprints;
69
+ async function loadFootprints() {
70
+ const ffi = await import(BUN_FFI);
71
+ if (!isBunFfi(ffi)) {
72
+ throw new Error(`${BUN_FFI} names no dlopen and ptr: a tree's footprint is read on Bun, by the peak waiter`);
73
+ }
74
+ const { symbols } = ffi.dlopen(LIBPROC, {
75
+ proc_listchildpids: { args: ['i32', 'ptr', 'i32'], returns: 'i32' },
76
+ proc_pid_rusage: { args: ['i32', 'i32', 'ptr'], returns: 'i32' },
77
+ });
78
+ if (!isLibprocSymbols(symbols)) {
79
+ throw new Error(`${LIBPROC} opened without proc_listchildpids and proc_pid_rusage`);
80
+ }
81
+ const usage = new BigUint64Array(RUSAGE_INFO_V0_WORDS);
82
+ const usageAt = ffi.ptr(usage);
83
+ let pids = new Int32Array(256);
84
+ return {
85
+ children(pid) {
86
+ // A full buffer may have cut the list short: it is read again into one twice the size.
87
+ for (;;) {
88
+ const count = symbols.proc_listchildpids(pid, ffi.ptr(pids), pids.byteLength);
89
+ if (count < pids.length)
90
+ return count <= 0 ? [] : Array.from(pids.subarray(0, count));
91
+ pids = new Int32Array(pids.length * 2);
92
+ }
93
+ },
94
+ footprint(pid) {
95
+ return symbols.proc_pid_rusage(pid, RUSAGE_INFO_V0, usageAt) === 0 ? Number(usage[PHYS_FOOTPRINT]) : 0;
96
+ },
97
+ };
98
+ }
99
+ /** What the tree rooted at `root` holds: each member's `ri_phys_footprint`, the tree walked by `proc_listchildpids`. */
100
+ async function treeResidentBytes(root) {
101
+ footprints ??= loadFootprints();
102
+ const proc = await footprints;
103
+ const seen = new Set();
104
+ const pending = [root];
105
+ let total = 0;
106
+ for (let pid = pending.pop(); pid !== undefined; pid = pending.pop()) {
107
+ if (seen.has(pid))
108
+ continue;
109
+ seen.add(pid);
110
+ total += proc.footprint(pid);
111
+ pending.push(...proc.children(pid));
112
+ }
113
+ return total;
114
+ }
115
+ export const darwinMemory = { readRunMemory, peakBytes: (maxRss) => maxRss, treeResidentBytes };
@@ -1 +1 @@
1
- {"version":3,"file":"host-memory.linux.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/host-memory.linux.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAA6B,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAchG,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAChB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AA2B5E,2DAA2D;AAC3D,wBAAsB,cAAc,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,CA8CzG;AAqBD,eAAO,MAAM,WAAW,EAAE,UAAyE,CAAC"}
1
+ {"version":3,"file":"host-memory.linux.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/host-memory.linux.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAA6B,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAchG,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAChB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AA2B5E,2DAA2D;AAC3D,wBAAsB,cAAc,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,CA8CzG;AAoDD,eAAO,MAAM,WAAW,EAAE,UAA4F,CAAC"}
@@ -1,5 +1,6 @@
1
1
  import { readFile } from 'node:fs/promises';
2
2
  import { posix } from 'node:path';
3
+ import { childPids } from '../../process-children.linux.js';
3
4
  /**
4
5
  * Linux's memory mechanisms. The run's memory is the least of the host's `MemTotal` and every finite `memory.max` of
5
6
  * the cgroup v2 the run's process is in and of its ancestors, up to the root of the mounted hierarchy: the kernel holds
@@ -101,4 +102,36 @@ function peakBytes(maxRss, bunVersion) {
101
102
  const [major, minor] = [Number(version[1]), Number(version[2])];
102
103
  return major < 1 || (major === 1 && minor < 4) ? maxRss * 1024 : maxRss;
103
104
  }
104
- export const linuxMemory = { readRunMemory: () => linuxRunMemory(readText), peakBytes };
105
+ /** What the tree rooted at `root` holds resident: each member's `VmRSS`, the tree walked by the kernel's child links. */
106
+ async function treeResidentBytes(root) {
107
+ const seen = new Set();
108
+ let level = [root];
109
+ let total = 0;
110
+ while (level.length > 0) {
111
+ const members = level.filter((pid) => !seen.has(pid));
112
+ for (const pid of members)
113
+ seen.add(pid);
114
+ const [resident, children] = await Promise.all([Promise.all(members.map(residentBytes)), childPids(members)]);
115
+ total += resident.reduce((sum, bytes) => sum + bytes, 0);
116
+ level = children;
117
+ }
118
+ return total;
119
+ }
120
+ /**
121
+ * `VmRSS` from `/proc/<pid>/status`: what the process holds resident, anonymous, file-backed and shared memory together.
122
+ * A process that has exited (`ENOENT`, `ESRCH`) holds nothing, and so does a zombie, whose status names no `VmRSS`.
123
+ */
124
+ async function residentBytes(pid) {
125
+ let status;
126
+ try {
127
+ status = await readFile(`/proc/${pid}/status`, 'utf8');
128
+ }
129
+ catch (error) {
130
+ if (error instanceof Error && 'code' in error && (error.code === 'ENOENT' || error.code === 'ESRCH'))
131
+ return 0;
132
+ throw error;
133
+ }
134
+ const kib = /^VmRSS:\s+(\d+) kB$/m.exec(status)?.[1];
135
+ return kib === undefined ? 0 : Number(kib) * 1024;
136
+ }
137
+ export const linuxMemory = { readRunMemory: () => linuxRunMemory(readText), peakBytes, treeResidentBytes };
@@ -2,10 +2,11 @@ import type { IOType } from 'node:child_process';
2
2
  import type { Readable } from 'node:stream';
3
3
  /**
4
4
  * A leaf's memory peak (specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets itself"; 13_telemetry.md, "Job
5
- * resource peaks"): the exited command's rusage `ru_maxrss`, which its waiter (`peak-waiter.ts`) reads. The leaf runs
6
- * no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold inside the
7
- * job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is the rusage on Linux and macOS alike. The executor writes
8
- * it into the task's record, and the task's next run is charged it in its run's pool.
5
+ * resource peaks"): the largest sum of what the exited command's process tree held side by side, as its waiter
6
+ * (`peak-waiter.ts`) sampled it, never less than the command's rusage `ru_maxrss`, which the waiter also reads. The
7
+ * leaf runs no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold
8
+ * inside the job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is read from the leaf's own tree on Linux and
9
+ * macOS alike. The executor writes it into the task's record, and the task's next run is charged it in its run's pool.
9
10
  */
10
11
  /** The descriptor the waiter reports on: past the lifecycle channel (3), the subtree admission (4) and mbx's grant (5). */
11
12
  export declare const PEAK_FD = 6;
@@ -1 +1 @@
1
- {"version":3,"file":"leaf-peak.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/leaf-peak.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAM5C;;;;;;GAMG;AAEH,2HAA2H;AAC3H,eAAO,MAAM,OAAO,IAAI,CAAC;AAiBzB,yDAAyD;AACzD,MAAM,MAAM,QAAQ,GAChB;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE7D,wEAAwE;AACxE,MAAM,MAAM,UAAU,GAClB;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE7D;;;GAGG;AACH,wBAAgB,UAAU,IAAI,UAAU,CASvC;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,WAAW,EAAE,SAAS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AACH,wBAAgB,UAAU,IAAI,UAAU,CASvC;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,WAAW,EAAE,SAAS,MAAM,EAAE,GAC7B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAanD;AAED,sDAAsD;AACtD,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACzC,gFAAgF;IAChF,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;CAClC;AAED,mHAAmH;AACnH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,QAAQ,GAAG,IAAI,GAAG,SAAS,GAAG,YAAY,CAuDlF"}
@@ -6,10 +6,11 @@ import { HOST_MEMORY } from './host-memory.js';
6
6
  import { resolveOnPath } from './runtime-pin.js';
7
7
  /**
8
8
  * A leaf's memory peak (specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets itself"; 13_telemetry.md, "Job
9
- * resource peaks"): the exited command's rusage `ru_maxrss`, which its waiter (`peak-waiter.ts`) reads. The leaf runs
10
- * no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold inside the
11
- * job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is the rusage on Linux and macOS alike. The executor writes
12
- * it into the task's record, and the task's next run is charged it in its run's pool.
9
+ * resource peaks"): the largest sum of what the exited command's process tree held side by side, as its waiter
10
+ * (`peak-waiter.ts`) sampled it, never less than the command's rusage `ru_maxrss`, which the waiter also reads. The
11
+ * leaf runs no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold
12
+ * inside the job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is read from the leaf's own tree on Linux and
13
+ * macOS alike. The executor writes it into the task's record, and the task's next run is charged it in its run's pool.
13
14
  */
14
15
  /** The descriptor the waiter reports on: past the lifecycle channel (3), the subtree admission (4) and mbx's grant (5). */
15
16
  export const PEAK_FD = 6;
@@ -9,6 +9,28 @@ import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src
9
9
  * until its connection closes, so an executor that dies gives back what it held.
10
10
  */
11
11
  export declare const RESERVATIONS_ENV = "MBX_RESERVATIONS_SOCKET";
12
+ /**
13
+ * Why the run serves its tasks no endpoint, published beside {@link RESERVATIONS_ENV} when its endpoint did not start.
14
+ * Every task inherits it, so each reserved leaf's refusal names the cause: a gate's failures-only output shows the
15
+ * leaves' refusals, never the run's own warning.
16
+ */
17
+ export declare const RESERVATIONS_UNSERVED_ENV = "BOUNDED_EXEC_RESERVATIONS_UNSERVED";
18
+ /** What a task's environment says of its run's reservation endpoint. */
19
+ export type RunEndpoint = {
20
+ readonly kind: 'served';
21
+ readonly socket: string;
22
+ } | {
23
+ readonly kind: 'unserved';
24
+ readonly reason: string;
25
+ } | {
26
+ readonly kind: 'unpublished';
27
+ };
28
+ /** The endpoint `env` names, else why its run serves none, else that its run published neither. */
29
+ export declare function runEndpoint(env: Readonly<NodeJS.ProcessEnv>): RunEndpoint;
30
+ /** Why a task's run serves it no endpoint, as its reserved leaf's refusal says it. */
31
+ export declare function describeUnserved(endpoint: Exclude<RunEndpoint, {
32
+ kind: 'served';
33
+ }>): string;
12
34
  /**
13
35
  * Serve `context`'s run an mbx reservation endpoint and publish it in `env`, which every task inherits, when the
14
36
  * environment names mbx's pool (`mbxPool`). An endpoint that does not start is said, and each reserved leaf of the run
@@ -1 +1 @@
1
- {"version":3,"file":"mbx-reservations.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/mbx-reservations.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAMtH;;;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ChF,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IACnC,6FAA6F;IAC7F,OAAO,IAAI,IAAI,CAAC;CACjB;AAgID;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,KAAK,GAAG,WAAW,CAUlG"}
@@ -21,6 +21,26 @@ import { mbxPool } from './mbx-pool.js';
21
21
  * until its connection closes, so an executor that dies gives back what it held.
22
22
  */
23
23
  export const RESERVATIONS_ENV = 'MBX_RESERVATIONS_SOCKET';
24
+ /**
25
+ * Why the run serves its tasks no endpoint, published beside {@link RESERVATIONS_ENV} when its endpoint did not start.
26
+ * Every task inherits it, so each reserved leaf's refusal names the cause: a gate's failures-only output shows the
27
+ * leaves' refusals, never the run's own warning.
28
+ */
29
+ export const RESERVATIONS_UNSERVED_ENV = 'BOUNDED_EXEC_RESERVATIONS_UNSERVED';
30
+ /** The endpoint `env` names, else why its run serves none, else that its run published neither. */
31
+ export function runEndpoint(env) {
32
+ const socket = env[RESERVATIONS_ENV] ?? '';
33
+ if (socket !== '')
34
+ return { kind: 'served', socket };
35
+ const reason = env[RESERVATIONS_UNSERVED_ENV] ?? '';
36
+ return reason === '' ? { kind: 'unpublished' } : { kind: 'unserved', reason };
37
+ }
38
+ /** Why a task's run serves it no endpoint, as its reserved leaf's refusal says it. */
39
+ export function describeUnserved(endpoint) {
40
+ return endpoint.kind === 'unserved'
41
+ ? `the run's endpoint did not start (${endpoint.reason})`
42
+ : `the run published neither an endpoint nor why it has none (${RESERVATIONS_UNSERVED_ENV} is unset too), so its preTasksExecution never served this run or its environment never reached this task`;
43
+ }
24
44
  const ENDPOINT_DIRECTORY = 'mbx-r-';
25
45
  const ENDPOINT_SOCKET = 'r.sock';
26
46
  /** What is kept of the endpoint's stderr to say why it ended: its last bytes. */
@@ -39,10 +59,12 @@ export async function serveRunReservations(context, env = process.env) {
39
59
  const started = await startEndpoint(env);
40
60
  if (!started.ok) {
41
61
  logger.warn(`bounded-exec: this run's mbx reservation endpoint did not start (${started.error}); its reserved leaves refuse to run`);
62
+ env[RESERVATIONS_UNSERVED_ENV] = started.error;
42
63
  return;
43
64
  }
44
65
  endpoints.set(context.id, started.endpoint);
45
66
  env[RESERVATIONS_ENV] = started.socket;
67
+ delete env[RESERVATIONS_UNSERVED_ENV];
46
68
  }
47
69
  /** Close the endpoint `context`'s run was served, if it was served one: its standard input's end ends it. */
48
70
  export async function closeRunReservations(context) {
@@ -8,12 +8,15 @@ function isBunSpawner(runtime) {
8
8
  'version' in runtime &&
9
9
  typeof runtime.version === 'string');
10
10
  }
11
+ /** How often, in ms, the waiter sums what its command's tree holds while the command runs. */
12
+ const SAMPLE_MS = 250;
11
13
  const IGNORED = ['SIGINT', 'SIGTERM', 'SIGHUP'];
12
14
  const bun = Reflect.get(globalThis, 'Bun');
13
15
  const [reportFd, list, file, ...args] = process.argv.slice(2);
14
16
  if (!isBunSpawner(bun) || HOST_MEMORY === null || reportFd === undefined || list === undefined || file === undefined) {
15
17
  throw new Error('peak-waiter runs on Bun, on Linux or macOS, as `peak-waiter.js <report fd> <fds> <file> [args…]`');
16
18
  }
19
+ const memory = HOST_MEMORY;
17
20
  const report = (line) => {
18
21
  try {
19
22
  writeSync(Number(reportFd), `${JSON.stringify(line)}\n`);
@@ -40,11 +43,34 @@ catch (error) {
40
43
  process.exit(1);
41
44
  }
42
45
  report({ started: child.pid });
46
+ const root = child.pid;
47
+ let treePeak = 0;
48
+ let unreadTree = null;
49
+ let reading = null;
50
+ const sample = async () => {
51
+ try {
52
+ treePeak = Math.max(treePeak, await memory.treeResidentBytes(root));
53
+ }
54
+ catch (error) {
55
+ unreadTree ??= error instanceof Error ? error.message : String(error);
56
+ }
57
+ };
58
+ // A read still running when the next period starts takes that period too: reads of one tree never overlap.
59
+ const sampler = setInterval(() => {
60
+ reading ??= sample().finally(() => {
61
+ reading = null;
62
+ });
63
+ }, SAMPLE_MS);
43
64
  await child.exited;
65
+ clearInterval(sampler);
66
+ await reading;
67
+ if (unreadTree !== null) {
68
+ writeSync(2, `bounded-exec: the command tree's resident sum could not be read (${unreadTree}); its peak is the larger of what was read and its largest process's ru_maxrss\n`);
69
+ }
44
70
  const usage = child.resourceUsage();
45
71
  report(usage === undefined
46
72
  ? { unmeasured: 'Bun read no resource usage for the exited command' }
47
- : { peakBytes: HOST_MEMORY.peakBytes(usage.maxRSS, bun.version) });
73
+ : { peakBytes: Math.max(treePeak, memory.peakBytes(usage.maxRSS, bun.version)) });
48
74
  if (child.signalCode !== null) {
49
75
  for (const signal of IGNORED)
50
76
  process.removeListener(signal, ignore);
@@ -83,17 +83,46 @@ runs:
83
83
  # Hash the exact identity (not a lossy path slug); keep cache artifacts
84
84
  # and their metadata DB together, warm across runs of the same pull
85
85
  # request or ref and job.
86
+ #
87
+ # A pull request's first run starts from its base branch's task records.
88
+ # Each bounded-exec leaf is charged its task's last recorded memory peak
89
+ # (specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets
90
+ # itself"), and a scope that holds no records charges every leaf nothing,
91
+ # so a pull request's first run would admit every leaf on CPU tokens
92
+ # alone. Its base branch's pushes keep their records in the base's scope:
93
+ # a pull request's scope that holds none takes a copy of each task's last
94
+ # record (its verdict.json, nothing beside it) from there, and its own
95
+ # runs replace them. The copy is the record itself, never a second store.
86
96
  - name: 📂 Locate the Nx cache on the shared bind
87
97
  if: steps.runner-kind.outputs.host == 'true'
88
98
  shell: bash
89
99
  env:
100
+ NX_CACHE_BIND: /var/cache/ci/nx
90
101
  NX_CACHE_REPO: ${{ github.repository }}
91
102
  NX_CACHE_LANE: ${{ github.workflow }}
92
103
  SCOPE_REF: ${{ github.event.pull_request.number || github.ref }}
104
+ # A pull request's base branch, whose pushes run under refs/heads/<it>;
105
+ # empty for a push.
106
+ BASE_REF: ${{ github.base_ref }}
93
107
  run: |
94
108
  lane=$(printf '%s' "$NX_CACHE_LANE" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9._-' '-')
95
- scope=$(printf '%s\0%s\0%s\0%s\0%s' "$GITHUB_WORKFLOW" "$SCOPE_REF" "$GITHUB_JOB" "$RUNNER_OS" "$RUNNER_ARCH" | git hash-object --stdin)
96
- NX_CACHE_ROOT="/var/cache/ci/nx/$NX_CACHE_REPO/${lane:-default}/$scope"
109
+ # The cache root of the run identity SCOPE_REF names.
110
+ scope_root() {
111
+ scope=$(printf '%s\0%s\0%s\0%s\0%s' "$GITHUB_WORKFLOW" "$SCOPE_REF" "$GITHUB_JOB" "$RUNNER_OS" "$RUNNER_ARCH" | git hash-object --stdin)
112
+ printf '%s' "$NX_CACHE_BIND/$NX_CACHE_REPO/${lane:-default}/$scope"
113
+ }
114
+ NX_CACHE_ROOT=$(scope_root)
115
+ records="$NX_CACHE_ROOT/workspace-data/bounded-exec"
116
+ if [ -n "$BASE_REF" ] && [ ! -d "$records" ]; then
117
+ base="$(SCOPE_REF="refs/heads/$BASE_REF" scope_root)/workspace-data/bounded-exec"
118
+ for verdict in "$base"/*/verdict.json; do
119
+ [ -e "$verdict" ] || continue
120
+ task=$(basename "$(dirname "$verdict")")
121
+ mkdir -p "$records/$task"
122
+ cp "$verdict" "$records/$task/verdict.json"
123
+ done
124
+ if [ -d "$records" ]; then echo "task records: from the $BASE_REF scope"; fi
125
+ fi
97
126
  mkdir -p "$NX_CACHE_ROOT/cache" "$NX_CACHE_ROOT/workspace-data"
98
127
  {
99
128
  echo "NX_CACHE_DIRECTORY=$NX_CACHE_ROOT/cache"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.7.6",
3
+ "version": "0.7.7",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -435,7 +435,7 @@
435
435
  "{workspaceRoot}/packages/cowshed/crates/cowshed-core/src/port_zero.cjs"
436
436
  ],
437
437
  "options": {
438
- "command": "bun test --timeout=0 ./src/executors/bounded-exec/__tests__/cpu-pool.silence.ts",
438
+ "command": "bun test --config=/dev/null --timeout=0 ./src/executors/bounded-exec/__tests__/cpu-pool.silence.ts",
439
439
  "cwd": "{projectRoot}",
440
440
  "idleTimeoutMs": 180000,
441
441
  "killAfterMs": 10000
@@ -1,7 +1,7 @@
1
1
  import { type ChildProcess, type IOType, spawn } from 'node:child_process';
2
2
  import { accessSync, constants } from 'node:fs';
3
3
  import { mkdtemp, rm } from 'node:fs/promises';
4
- import { availableParallelism, userInfo } from 'node:os';
4
+ import { availableParallelism, constants as osConstants, userInfo } from 'node:os';
5
5
  import { delimiter, isAbsolute, join, resolve as resolvePath } from 'node:path';
6
6
  import { createInterface } from 'node:readline';
7
7
  import { Duplex, Readable } from 'node:stream';
@@ -49,7 +49,7 @@ import {
49
49
  withMbxLeaf,
50
50
  } from './mbx-pool.js';
51
51
  import { MBX_QUEUE_POLL_MS, type MbxQueuedRequest, snapshotMbxQueue } from './mbx-queue.js';
52
- import { RESERVATIONS_ENV, type Reservation, reserveCores } from './mbx-reservations.js';
52
+ import { describeUnserved, RESERVATIONS_ENV, type Reservation, reserveCores, runEndpoint } from './mbx-reservations.js';
53
53
  import { createNoSpaceWatch } from './no-space.js';
54
54
  import {
55
55
  COWSHED_EXECUTABLE_ENV,
@@ -105,12 +105,6 @@ type IdleDiagnosticResult =
105
105
  | { kind: 'pending'; startedAtMs: number | null }
106
106
  | { kind: 'captured'; snapshot: HangDiagnostics }
107
107
  | { kind: 'failed'; detail: string };
108
- const EXIT_CODE_BY_SIGNAL: Partial<Record<NodeJS.Signals, number>> = {
109
- SIGHUP: 129,
110
- SIGINT: 130,
111
- SIGTERM: 143,
112
- };
113
-
114
108
  /** The pool descriptor: only a grant may give it, so neither the target's nor the inherited environment passes it on. */
115
109
  const POOL_ENV = [CPU_TOKENS_ENV, CPU_POOL_ENV];
116
110
  /**
@@ -715,13 +709,15 @@ export async function runBoundedExec(
715
709
  return refuse(`cowshed: cpu-pool parent pool refused: ${parentRefusal}; refusing to run the command\n`);
716
710
  }
717
711
  // A reserved leaf takes its cores from its Nx run's mbx reservation endpoint (mbx-reservations.ts). A run that serves
718
- // none said why in its own output; the leaf does not run beside the pool.
719
- const reserved = leaf?.kind === 'reserve' ? { leaf, endpoint: process.env[RESERVATIONS_ENV] ?? '' } : null;
720
- if (reserved !== null && reserved.endpoint === '') {
712
+ // none said why in its own output, which a gate's failures-only output never shows, so the refusal says it too; the
713
+ // leaf does not run beside the pool.
714
+ const endpoint = leaf?.kind === 'reserve' ? runEndpoint(process.env) : null;
715
+ if (endpoint !== null && endpoint.kind !== 'served') {
721
716
  return refuse(
722
- `cowshed: a reserved leaf takes its cores from its Nx run's mbx reservation endpoint, and ${RESERVATIONS_ENV} names none (the run's preTasksExecution serves it); refusing to run the command\n`,
717
+ `cowshed: a reserved leaf takes its cores from its Nx run's mbx reservation endpoint, and ${RESERVATIONS_ENV} names none: ${describeUnserved(endpoint)}; refusing to run the command\n`,
723
718
  );
724
719
  }
720
+ const reserved = leaf?.kind === 'reserve' && endpoint?.kind === 'served' ? { leaf, endpoint: endpoint.socket } : null;
725
721
 
726
722
  // A granted run serves a sub-pool of its own grant, so its descendants draw from that and never from above it.
727
723
  // A pool that cannot be set up refuses the run: no granted command runs beside its grant.
@@ -1448,10 +1444,16 @@ export async function runBoundedExec(
1448
1444
 
1449
1445
  const code = exitCode ?? signalToExitCode(exitSignal);
1450
1446
  if (code !== 0 && state.expiry === null) {
1447
+ // No bound of this executor ended it, so a signal it died of came from outside (the kernel's OOM killer sends
1448
+ // SIGKILL): the signal is named, never folded into a bare status.
1449
+ const ended =
1450
+ exitCode === null && exitSignal !== null
1451
+ ? `was killed by ${exitSignal} (status ${code})`
1452
+ : `exited with status ${code}`;
1451
1453
  appendStderr(
1452
1454
  fixture !== null && !mainStarted
1453
- ? `Fixture command exited with status ${code}: ${segment ?? fixture.command}\n`
1454
- : `Command exited with status ${code}: ${command}\n`,
1455
+ ? `Fixture command ${ended}: ${segment ?? fixture.command}\n`
1456
+ : `Command ${ended}: ${command}\n`,
1455
1457
  );
1456
1458
  }
1457
1459
 
@@ -1550,11 +1552,9 @@ function targetEnv(env: Record<string, string> | undefined, say: (text: string)
1550
1552
  return kept;
1551
1553
  }
1552
1554
 
1555
+ /** The status a shell gives a command a signal ended: 128 plus the signal's number (SIGKILL 137, SIGTERM 143). */
1553
1556
  function signalToExitCode(signal: NodeJS.Signals | null): number {
1554
- if (!signal) {
1555
- return 1;
1556
- }
1557
- return EXIT_CODE_BY_SIGNAL[signal] ?? 1;
1557
+ return signal === null ? 1 : 128 + osConstants.signals[signal];
1558
1558
  }
1559
1559
 
1560
1560
  /** Resolves when `promise` settles or `ms` passes, whichever is first, leaving no timer behind. */
@@ -41,4 +41,107 @@ async function readRunMemory(): Promise<RunMemoryReading> {
41
41
  return { ok: true, memory: { bytes, source: HW_MEMSIZE } };
42
42
  }
43
43
 
44
- export const darwinMemory: HostMemory = { readRunMemory, peakBytes: (maxRss) => maxRss };
44
+ /**
45
+ * Bun's FFI, as the peak waiter reaches it. Node, which runs the executor, loads this module too and has no `bun:ffi`;
46
+ * only the waiter, on Bun, reads a tree's footprint, so the FFI loads on its first read, by a specifier the compiler
47
+ * does not resolve against Node's types.
48
+ */
49
+ const BUN_FFI = 'bun:ffi';
50
+ const LIBPROC = '/usr/lib/libproc.dylib';
51
+ /** `proc_pid_rusage`'s oldest flavor, whose fields every later one starts with. */
52
+ const RUSAGE_INFO_V0 = 0;
53
+ /** `rusage_info_v0`: a 16-byte uuid, then ten u64 counters; `ri_phys_footprint` is the eighth, u64 index 9. */
54
+ const RUSAGE_INFO_V0_WORDS = 12;
55
+ const PHYS_FOOTPRINT = 9;
56
+
57
+ interface BunFfi {
58
+ dlopen(path: string, symbols: Record<string, { args: string[]; returns: string }>): { symbols: unknown };
59
+ ptr(view: ArrayBufferView): number;
60
+ }
61
+
62
+ /** The two libproc calls a tree's footprint takes, each pointer a Bun FFI address. */
63
+ interface LibprocSymbols {
64
+ /** The pids whose parent is `pid`, written into `into` (`bytes` long): how many, or -1. */
65
+ proc_listchildpids(pid: number, into: number, bytes: number): number;
66
+ /** `pid`'s `rusage_info` of `flavor`, written into `into`: 0, or -1 once nothing it may read holds the pid. */
67
+ proc_pid_rusage(pid: number, flavor: number, into: number): number;
68
+ }
69
+
70
+ function isBunFfi(module: unknown): module is BunFfi {
71
+ return (
72
+ typeof module === 'object' &&
73
+ module !== null &&
74
+ 'dlopen' in module &&
75
+ typeof module.dlopen === 'function' &&
76
+ 'ptr' in module &&
77
+ typeof module.ptr === 'function'
78
+ );
79
+ }
80
+
81
+ function isLibprocSymbols(symbols: unknown): symbols is LibprocSymbols {
82
+ return (
83
+ typeof symbols === 'object' &&
84
+ symbols !== null &&
85
+ 'proc_listchildpids' in symbols &&
86
+ typeof symbols.proc_listchildpids === 'function' &&
87
+ 'proc_pid_rusage' in symbols &&
88
+ typeof symbols.proc_pid_rusage === 'function'
89
+ );
90
+ }
91
+
92
+ /** One process's children and footprint, as libproc reads them. */
93
+ interface Footprints {
94
+ children(pid: number): number[];
95
+ /** What `pid` holds, or 0 once it is gone (or, a setuid program, unreadable to its owner's uid). */
96
+ footprint(pid: number): number;
97
+ }
98
+
99
+ let footprints: Promise<Footprints> | undefined;
100
+
101
+ async function loadFootprints(): Promise<Footprints> {
102
+ const ffi: unknown = await import(BUN_FFI);
103
+ if (!isBunFfi(ffi)) {
104
+ throw new Error(`${BUN_FFI} names no dlopen and ptr: a tree's footprint is read on Bun, by the peak waiter`);
105
+ }
106
+ const { symbols } = ffi.dlopen(LIBPROC, {
107
+ proc_listchildpids: { args: ['i32', 'ptr', 'i32'], returns: 'i32' },
108
+ proc_pid_rusage: { args: ['i32', 'i32', 'ptr'], returns: 'i32' },
109
+ });
110
+ if (!isLibprocSymbols(symbols)) {
111
+ throw new Error(`${LIBPROC} opened without proc_listchildpids and proc_pid_rusage`);
112
+ }
113
+ const usage = new BigUint64Array(RUSAGE_INFO_V0_WORDS);
114
+ const usageAt = ffi.ptr(usage);
115
+ let pids = new Int32Array(256);
116
+ return {
117
+ children(pid) {
118
+ // A full buffer may have cut the list short: it is read again into one twice the size.
119
+ for (;;) {
120
+ const count = symbols.proc_listchildpids(pid, ffi.ptr(pids), pids.byteLength);
121
+ if (count < pids.length) return count <= 0 ? [] : Array.from(pids.subarray(0, count));
122
+ pids = new Int32Array(pids.length * 2);
123
+ }
124
+ },
125
+ footprint(pid) {
126
+ return symbols.proc_pid_rusage(pid, RUSAGE_INFO_V0, usageAt) === 0 ? Number(usage[PHYS_FOOTPRINT]) : 0;
127
+ },
128
+ };
129
+ }
130
+
131
+ /** What the tree rooted at `root` holds: each member's `ri_phys_footprint`, the tree walked by `proc_listchildpids`. */
132
+ async function treeResidentBytes(root: number): Promise<number> {
133
+ footprints ??= loadFootprints();
134
+ const proc = await footprints;
135
+ const seen = new Set<number>();
136
+ const pending = [root];
137
+ let total = 0;
138
+ for (let pid = pending.pop(); pid !== undefined; pid = pending.pop()) {
139
+ if (seen.has(pid)) continue;
140
+ seen.add(pid);
141
+ total += proc.footprint(pid);
142
+ pending.push(...proc.children(pid));
143
+ }
144
+ return total;
145
+ }
146
+
147
+ export const darwinMemory: HostMemory = { readRunMemory, peakBytes: (maxRss) => maxRss, treeResidentBytes };
@@ -1,5 +1,6 @@
1
1
  import { readFile } from 'node:fs/promises';
2
2
  import { posix } from 'node:path';
3
+ import { childPids } from '../../process-children.linux.js';
3
4
  import type { HostMemory, RunMemory, RunMemoryError, RunMemoryReading } from './host-memory.js';
4
5
 
5
6
  /**
@@ -112,4 +113,35 @@ function peakBytes(maxRss: number, bunVersion: string): number {
112
113
  return major < 1 || (major === 1 && minor < 4) ? maxRss * 1024 : maxRss;
113
114
  }
114
115
 
115
- export const linuxMemory: HostMemory = { readRunMemory: () => linuxRunMemory(readText), peakBytes };
116
+ /** What the tree rooted at `root` holds resident: each member's `VmRSS`, the tree walked by the kernel's child links. */
117
+ async function treeResidentBytes(root: number): Promise<number> {
118
+ const seen = new Set<number>();
119
+ let level = [root];
120
+ let total = 0;
121
+ while (level.length > 0) {
122
+ const members = level.filter((pid) => !seen.has(pid));
123
+ for (const pid of members) seen.add(pid);
124
+ const [resident, children] = await Promise.all([Promise.all(members.map(residentBytes)), childPids(members)]);
125
+ total += resident.reduce((sum, bytes) => sum + bytes, 0);
126
+ level = children;
127
+ }
128
+ return total;
129
+ }
130
+
131
+ /**
132
+ * `VmRSS` from `/proc/<pid>/status`: what the process holds resident, anonymous, file-backed and shared memory together.
133
+ * A process that has exited (`ENOENT`, `ESRCH`) holds nothing, and so does a zombie, whose status names no `VmRSS`.
134
+ */
135
+ async function residentBytes(pid: number): Promise<number> {
136
+ let status: string;
137
+ try {
138
+ status = await readFile(`/proc/${pid}/status`, 'utf8');
139
+ } catch (error) {
140
+ if (error instanceof Error && 'code' in error && (error.code === 'ENOENT' || error.code === 'ESRCH')) return 0;
141
+ throw error;
142
+ }
143
+ const kib = /^VmRSS:\s+(\d+) kB$/m.exec(status)?.[1];
144
+ return kib === undefined ? 0 : Number(kib) * 1024;
145
+ }
146
+
147
+ export const linuxMemory: HostMemory = { readRunMemory: () => linuxRunMemory(readText), peakBytes, treeResidentBytes };
@@ -14,6 +14,13 @@ export interface HostMemory {
14
14
  * it: the exited leaf's `ru_maxrss`.
15
15
  */
16
16
  peakBytes(maxRss: number, bunVersion: string): number;
17
+ /**
18
+ * What the process tree rooted at `root` holds resident at this instant: the sum over `root` and every descendant the
19
+ * kernel links to it, each process's footprint where the platform keeps one (macOS `ri_phys_footprint`, which counts
20
+ * its compressed pages) and its resident set where it does not (Linux `VmRSS`). A process that exits between the
21
+ * listing and its read adds nothing, and a root that has exited holds nothing.
22
+ */
23
+ treeResidentBytes(root: number): Promise<number>;
17
24
  }
18
25
 
19
26
  /** The run's memory in bytes, and where it was read: the file or sysctl that holds it. */
@@ -7,10 +7,11 @@ import { resolveOnPath } from './runtime-pin.js';
7
7
 
8
8
  /**
9
9
  * A leaf's memory peak (specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets itself"; 13_telemetry.md, "Job
10
- * resource peaks"): the exited command's rusage `ru_maxrss`, which its waiter (`peak-waiter.ts`) reads. The leaf runs
11
- * no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold inside the
12
- * job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is the rusage on Linux and macOS alike. The executor writes
13
- * it into the task's record, and the task's next run is charged it in its run's pool.
10
+ * resource peaks"): the largest sum of what the exited command's process tree held side by side, as its waiter
11
+ * (`peak-waiter.ts`) sampled it, never less than the command's rusage `ru_maxrss`, which the waiter also reads. The
12
+ * leaf runs no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold
13
+ * inside the job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is read from the leaf's own tree on Linux and
14
+ * macOS alike. The executor writes it into the task's record, and the task's next run is charged it in its run's pool.
14
15
  */
15
16
 
16
17
  /** The descriptor the waiter reports on: past the lifecycle channel (3), the subtree admission (4) and mbx's grant (5). */
@@ -21,6 +21,34 @@ import { mbxPool } from './mbx-pool.js';
21
21
  */
22
22
  export const RESERVATIONS_ENV = 'MBX_RESERVATIONS_SOCKET';
23
23
 
24
+ /**
25
+ * Why the run serves its tasks no endpoint, published beside {@link RESERVATIONS_ENV} when its endpoint did not start.
26
+ * Every task inherits it, so each reserved leaf's refusal names the cause: a gate's failures-only output shows the
27
+ * leaves' refusals, never the run's own warning.
28
+ */
29
+ export const RESERVATIONS_UNSERVED_ENV = 'BOUNDED_EXEC_RESERVATIONS_UNSERVED';
30
+
31
+ /** What a task's environment says of its run's reservation endpoint. */
32
+ export type RunEndpoint =
33
+ | { readonly kind: 'served'; readonly socket: string }
34
+ | { readonly kind: 'unserved'; readonly reason: string }
35
+ | { readonly kind: 'unpublished' };
36
+
37
+ /** The endpoint `env` names, else why its run serves none, else that its run published neither. */
38
+ export function runEndpoint(env: Readonly<NodeJS.ProcessEnv>): RunEndpoint {
39
+ const socket = env[RESERVATIONS_ENV] ?? '';
40
+ if (socket !== '') return { kind: 'served', socket };
41
+ const reason = env[RESERVATIONS_UNSERVED_ENV] ?? '';
42
+ return reason === '' ? { kind: 'unpublished' } : { kind: 'unserved', reason };
43
+ }
44
+
45
+ /** Why a task's run serves it no endpoint, as its reserved leaf's refusal says it. */
46
+ export function describeUnserved(endpoint: Exclude<RunEndpoint, { kind: 'served' }>): string {
47
+ return endpoint.kind === 'unserved'
48
+ ? `the run's endpoint did not start (${endpoint.reason})`
49
+ : `the run published neither an endpoint nor why it has none (${RESERVATIONS_UNSERVED_ENV} is unset too), so its preTasksExecution never served this run or its environment never reached this task`;
50
+ }
51
+
24
52
  const ENDPOINT_DIRECTORY = 'mbx-r-';
25
53
  const ENDPOINT_SOCKET = 'r.sock';
26
54
  /** What is kept of the endpoint's stderr to say why it ended: its last bytes. */
@@ -55,10 +83,12 @@ export async function serveRunReservations(
55
83
  logger.warn(
56
84
  `bounded-exec: this run's mbx reservation endpoint did not start (${started.error}); its reserved leaves refuse to run`,
57
85
  );
86
+ env[RESERVATIONS_UNSERVED_ENV] = started.error;
58
87
  return;
59
88
  }
60
89
  endpoints.set(context.id, started.endpoint);
61
90
  env[RESERVATIONS_ENV] = started.socket;
91
+ delete env[RESERVATIONS_UNSERVED_ENV];
62
92
  }
63
93
 
64
94
  /** Close the endpoint `context`'s run was served, if it was served one: its standard input's end ends it. */
@@ -6,9 +6,14 @@ import { HOST_MEMORY } from './host-memory.js';
6
6
  * on Bun as `bun --config=/dev/null peak-waiter.js <report fd> <fds> <file> [args…]`, it spawns `file args` with its
7
7
  * own stdin, stdout and stderr and each descriptor `fds` names (comma-separated) at its own number, writes the
8
8
  * command's pid to the report descriptor, waits for the command, writes its peak there, each as one JSON line, and
9
- * ends as the command did: with its exit code, or with its signal raised on itself. Node, which runs the executor,
10
- * reaps a child without its rusage; Bun's `Subprocess.resourceUsage()` reports it: `ru_maxrss`, the largest resident
11
- * set any one process of the tree it waited on reached.
9
+ * ends as the command did: with its exit code, or with its signal raised on itself.
10
+ *
11
+ * The peak is what the command's tree held side by side. Every {@link SAMPLE_MS} while the command runs, the waiter
12
+ * sums what the command and each descendant the kernel links to it hold (`HostMemory.treeResidentBytes`) and keeps the
13
+ * largest sum. At the exit it takes the larger of that and the command's `ru_maxrss`, the largest resident set any one
14
+ * process of the tree it waited on reached, which a spike between two samples still reaches: Node, which runs the
15
+ * executor, reaps a child without its rusage; Bun's `Subprocess.resourceUsage()` reports it. A tree it could not read
16
+ * is said on the command's stderr, and its peak is then the larger of what was read and `ru_maxrss`.
12
17
  *
13
18
  * The waiter leads the leaf's process group, so the executor's group signals reach the command directly: it ignores
14
19
  * SIGINT, SIGTERM and SIGHUP and outlives the command to report it. A SIGKILL of the group ends it unreported. The
@@ -40,12 +45,15 @@ function isBunSpawner(runtime: unknown): runtime is BunSpawner {
40
45
  );
41
46
  }
42
47
 
48
+ /** How often, in ms, the waiter sums what its command's tree holds while the command runs. */
49
+ const SAMPLE_MS = 250;
43
50
  const IGNORED = ['SIGINT', 'SIGTERM', 'SIGHUP'] as const;
44
51
  const bun: unknown = Reflect.get(globalThis, 'Bun');
45
52
  const [reportFd, list, file, ...args] = process.argv.slice(2);
46
53
  if (!isBunSpawner(bun) || HOST_MEMORY === null || reportFd === undefined || list === undefined || file === undefined) {
47
54
  throw new Error('peak-waiter runs on Bun, on Linux or macOS, as `peak-waiter.js <report fd> <fds> <file> [args…]`');
48
55
  }
56
+ const memory = HOST_MEMORY;
49
57
  const report = (line: { started: number } | { peakBytes: number } | { unmeasured: string }): void => {
50
58
  try {
51
59
  writeSync(Number(reportFd), `${JSON.stringify(line)}\n`);
@@ -68,12 +76,37 @@ try {
68
76
  process.exit(1);
69
77
  }
70
78
  report({ started: child.pid });
79
+ const root = child.pid;
80
+ let treePeak = 0;
81
+ let unreadTree: string | null = null;
82
+ let reading: Promise<void> | null = null;
83
+ const sample = async (): Promise<void> => {
84
+ try {
85
+ treePeak = Math.max(treePeak, await memory.treeResidentBytes(root));
86
+ } catch (error) {
87
+ unreadTree ??= error instanceof Error ? error.message : String(error);
88
+ }
89
+ };
90
+ // A read still running when the next period starts takes that period too: reads of one tree never overlap.
91
+ const sampler = setInterval(() => {
92
+ reading ??= sample().finally(() => {
93
+ reading = null;
94
+ });
95
+ }, SAMPLE_MS);
71
96
  await child.exited;
97
+ clearInterval(sampler);
98
+ await reading;
99
+ if (unreadTree !== null) {
100
+ writeSync(
101
+ 2,
102
+ `bounded-exec: the command tree's resident sum could not be read (${unreadTree}); its peak is the larger of what was read and its largest process's ru_maxrss\n`,
103
+ );
104
+ }
72
105
  const usage = child.resourceUsage();
73
106
  report(
74
107
  usage === undefined
75
108
  ? { unmeasured: 'Bun read no resource usage for the exited command' }
76
- : { peakBytes: HOST_MEMORY.peakBytes(usage.maxRSS, bun.version) },
109
+ : { peakBytes: Math.max(treePeak, memory.peakBytes(usage.maxRSS, bun.version)) },
77
110
  );
78
111
  if (child.signalCode !== null) {
79
112
  for (const signal of IGNORED) process.removeListener(signal, ignore);
package/test-timings.json CHANGED
@@ -52,6 +52,7 @@
52
52
  "src/task-history-namespace.test.ts": 1786,
53
53
  "src/managed-files/repo-path.test.ts": 1593,
54
54
  "src/nx-cache-kept-outputs.test.ts": 1363,
55
+ "src/managed-files/setup-devenv-task-records.test.ts": 1330,
55
56
  "src/nx-foreign-workspace-env.test.ts": 1255,
56
57
  "src/managed-files/forgejo-artifact.test.ts": 1192,
57
58
  "src/nx-plugin-load-errors.test.ts": 1180,
@@ -94,6 +95,7 @@
94
95
  "src/release-config-policy.test.ts": 72,
95
96
  "src/managed-files/cache-ttsc-plugins.test.ts": 62,
96
97
  "src/workspace-config-policy.test.ts": 61,
98
+ "src/executors/bounded-exec/host-memory.test.ts": 59,
97
99
  "src/generators/make-public/generator.test.ts": 52,
98
100
  "src/executors/bounded-exec/mbx-pool.test.ts": 46,
99
101
  "src/executors/scriptc-native/executor.test.ts": 44,