@smoothbricks/nx-plugin 0.7.4 → 0.7.6

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 (63) hide show
  1. package/README.md +59 -30
  2. package/dist/.build-inputs.json +15 -8
  3. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts +3 -2
  4. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts.map +1 -1
  5. package/dist/executors/bounded-exec/cpu-pool-thread.js +1 -1
  6. package/dist/executors/bounded-exec/cpu-pool.d.ts +24 -10
  7. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  8. package/dist/executors/bounded-exec/cpu-pool.js +96 -52
  9. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  10. package/dist/executors/bounded-exec/executor.js +192 -82
  11. package/dist/executors/bounded-exec/host-memory.d.ts +47 -0
  12. package/dist/executors/bounded-exec/host-memory.d.ts.map +1 -0
  13. package/dist/executors/bounded-exec/host-memory.darwin.d.ts +3 -0
  14. package/dist/executors/bounded-exec/host-memory.darwin.d.ts.map +1 -0
  15. package/dist/executors/bounded-exec/host-memory.darwin.js +40 -0
  16. package/dist/executors/bounded-exec/host-memory.js +29 -0
  17. package/dist/executors/bounded-exec/host-memory.linux.d.ts +14 -0
  18. package/dist/executors/bounded-exec/host-memory.linux.d.ts.map +1 -0
  19. package/dist/executors/bounded-exec/host-memory.linux.js +104 -0
  20. package/dist/executors/bounded-exec/leaf-peak.d.ts +55 -0
  21. package/dist/executors/bounded-exec/leaf-peak.d.ts.map +1 -0
  22. package/dist/executors/bounded-exec/leaf-peak.js +146 -0
  23. package/dist/executors/bounded-exec/mbx-pool.d.ts +17 -9
  24. package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
  25. package/dist/executors/bounded-exec/mbx-pool.js +29 -11
  26. package/dist/executors/bounded-exec/mbx-reservations.d.ts +41 -0
  27. package/dist/executors/bounded-exec/mbx-reservations.d.ts.map +1 -0
  28. package/dist/executors/bounded-exec/mbx-reservations.js +372 -0
  29. package/dist/executors/bounded-exec/peak-waiter.d.ts +2 -0
  30. package/dist/executors/bounded-exec/peak-waiter.d.ts.map +1 -0
  31. package/dist/executors/bounded-exec/peak-waiter.js +53 -0
  32. package/dist/executors/bounded-exec/pool-admission.d.ts +38 -0
  33. package/dist/executors/bounded-exec/pool-admission.d.ts.map +1 -0
  34. package/dist/executors/bounded-exec/pool-admission.js +31 -0
  35. package/dist/executors/bounded-exec/run-cpu-pool.d.ts +6 -4
  36. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  37. package/dist/executors/bounded-exec/run-cpu-pool.js +20 -7
  38. package/dist/executors/bounded-exec/runner-sizing.d.ts +5 -5
  39. package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -1
  40. package/dist/executors/bounded-exec/runner-sizing.js +6 -6
  41. package/dist/executors/bounded-exec/verdict.d.ts +21 -2
  42. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  43. package/dist/executors/bounded-exec/verdict.js +27 -4
  44. package/dist/index.d.ts +1 -1
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +4 -1
  47. package/package.json +1 -1
  48. package/src/executors/bounded-exec/cpu-pool-thread.ts +4 -3
  49. package/src/executors/bounded-exec/cpu-pool.ts +111 -49
  50. package/src/executors/bounded-exec/executor.ts +202 -77
  51. package/src/executors/bounded-exec/host-memory.darwin.ts +44 -0
  52. package/src/executors/bounded-exec/host-memory.linux.ts +115 -0
  53. package/src/executors/bounded-exec/host-memory.ts +70 -0
  54. package/src/executors/bounded-exec/leaf-peak.ts +151 -0
  55. package/src/executors/bounded-exec/mbx-pool.ts +33 -13
  56. package/src/executors/bounded-exec/mbx-reservations.ts +300 -0
  57. package/src/executors/bounded-exec/peak-waiter.ts +82 -0
  58. package/src/executors/bounded-exec/pool-admission.ts +64 -0
  59. package/src/executors/bounded-exec/run-cpu-pool.ts +20 -6
  60. package/src/executors/bounded-exec/runner-sizing.ts +6 -6
  61. package/src/executors/bounded-exec/verdict.ts +40 -6
  62. package/src/index.ts +4 -1
  63. package/test-timings.json +134 -131
@@ -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;AAIhF,OAAO,EAA4C,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAE5F,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,EAerB,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,CAAC,CAAA;KAAE,CAAC;CAC1D;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;CAC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G,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,CAopC5B;AAED,wBAAgB,uBAAuB,IAAI,iBAAiB,CAM3D"}
@@ -11,8 +11,10 @@ import { admissionWaitMs, earliestBound, NO_ADMISSION_WAIT, stepAdmissionWait, s
11
11
  import { CPU_POOL_ENV, createCpuPool, readInheritedCpuPool, takeParentCpuTokens, } from './cpu-pool.js';
12
12
  import { GATEWAY_SOCKET } from './gateway-lease.js';
13
13
  import { captureHangDiagnostics, HANG_CAPTURE_MS } from './hang-diagnostics.js';
14
- import { MBX_GRANT_FD, mbxIdleGated, mbxLeaf, mbxPool, withCountedReservation, withMbxLeaf } from './mbx-pool.js';
14
+ import { PEAK_FD, peakWaiter, readWaiterReport, underWaiter } from './leaf-peak.js';
15
+ import { askedCores, MBX_GRANT_FD, mbxIdleGated, mbxLeaf, mbxPool, withCountedReservation, withMbxLeaf, } from './mbx-pool.js';
15
16
  import { MBX_QUEUE_POLL_MS, snapshotMbxQueue } from './mbx-queue.js';
17
+ import { RESERVATIONS_ENV, reserveCores } from './mbx-reservations.js';
16
18
  import { createNoSpaceWatch } from './no-space.js';
17
19
  import { COWSHED_EXECUTABLE_ENV, cowshedSubtreeOwner, HANDLE_FD, readAdmission, SUPERVISOR_SOCKET_ENV, } from './owned-subtree.js';
18
20
  import { createRunProgress, withProgressReport, withTestResultReport } from './progress.js';
@@ -21,7 +23,7 @@ import { createRunnerLifecycle, withFixtureLifecycle, withRunnerLifecycle } from
21
23
  import { CPU_TOKENS_ENV, runnerDemand, sizeRunner, sizeRunnerToGrant } from './runner-sizing.js';
22
24
  import { checkRuntimePin, describeRuntimePinRefusal } from './runtime-pin.js';
23
25
  import { createTestResultWriter } from './test-result-writer.js';
24
- import { BUN_TEST, clearTaskDirectory, describeAdmissionQueue, describeNoSpace, JUNIT_FILE, JUNIT_REPORT_ENV, judgeOperationalFailure, judgeRun, readJunitReport, taskDirectory, withJunitReport, writeRecord, } from './verdict.js';
26
+ import { BUN_TEST, clearTaskDirectory, describeAdmissionQueue, describeNoSpace, JUNIT_FILE, JUNIT_REPORT_ENV, judgeOperationalFailure, judgeRun, readJunitReport, readLastPeak, taskDirectory, withJunitReport, writeRecord, } from './verdict.js';
25
27
  const DEFAULT_KILL_AFTER_MS = 10_000;
26
28
  const REAP_AFTER_FORCE_KILL_MS = 2_000;
27
29
  const EXIT_CODE_BY_SIGNAL = {
@@ -147,6 +149,8 @@ export function recordContext(context) {
147
149
  export async function runBoundedExec(options, context, killer, tempVolume, inherited = null, captureDiagnostics = captureHangDiagnostics, owner = null, queueHost) {
148
150
  const cwd = resolveCwd(options.cwd, context.root);
149
151
  const directory = context.record?.directory ?? null;
152
+ // The task's last recorded peak is its charge in its parent's pool, read before the clear below removes it.
153
+ const lastPeak = directory !== null && inherited !== null ? await readLastPeak(directory) : null;
150
154
  // A previous run's verdict and report go first, so neither can be read as this run's.
151
155
  if (directory !== null) {
152
156
  await clearTaskDirectory(directory);
@@ -195,17 +199,26 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
195
199
  // Nobody is left to tell.
196
200
  }
197
201
  };
202
+ // A run refused before its command exists says why and records an operational failure, and no peak: nothing ran.
203
+ const refuseUnstarted = async (message) => {
204
+ appendStderr(message);
205
+ if (context.record !== undefined) {
206
+ const { task, directory: recordDirectory } = context.record;
207
+ await writeRecord(recordDirectory, {
208
+ task: task.id,
209
+ hash: task.hash,
210
+ verdict: judgeOperationalFailure(1, null),
211
+ peakBytes: null,
212
+ });
213
+ }
214
+ return { success: false, terminalOutput: outputChunks.join('') };
215
+ };
198
216
  // The fixture pair names one segment: a fixture with no build budget, or a budget with no fixture, is a
199
217
  // target that does not say what it runs, so it does not run. Units are counted at the end of that segment.
200
218
  if ((options.fixtureCommand === undefined) !== (options.fixtureTimeoutMs === undefined) ||
201
219
  (options.unitsCommand !== undefined && options.fixtureCommand === undefined) ||
202
220
  (fixture !== null && !(fixture.timeoutMs > 0))) {
203
- appendStderr('bounded-exec: fixtureCommand and a positive fixtureTimeoutMs must be named together, and unitsCommand only with them; refusing to run the command\n');
204
- if (context.record !== undefined) {
205
- const { task, directory: recordDirectory } = context.record;
206
- await writeRecord(recordDirectory, { task: task.id, hash: task.hash, verdict: judgeOperationalFailure(1, null) });
207
- }
208
- return { success: false, terminalOutput: outputChunks.join('') };
221
+ return refuseUnstarted('bounded-exec: fixtureCommand and a positive fixtureTimeoutMs must be named together, and unitsCommand only with them; refusing to run the command\n');
209
222
  }
210
223
  // A leg that runs bun runs the Bun its workspace declares, or it does not run. This comes before the RAM lease and the
211
224
  // CPU tokens: a refusal that waited in its pool's queue first would say nothing for minutes.
@@ -217,13 +230,13 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
217
230
  pathSource: options.env?.PATH === undefined ? 'the inherited environment' : "the target's env",
218
231
  });
219
232
  if (runtime !== null) {
220
- appendStderr(describeRuntimePinRefusal(runtime, context.record?.task.id));
221
- if (context.record !== undefined) {
222
- const { task, directory: recordDirectory } = context.record;
223
- await writeRecord(recordDirectory, { task: task.id, hash: task.hash, verdict: judgeOperationalFailure(1, null) });
224
- }
225
- return { success: false, terminalOutput: outputChunks.join('') };
233
+ return refuseUnstarted(describeRuntimePinRefusal(runtime, context.record?.task.id));
234
+ }
235
+ if (lastPeak !== null && !lastPeak.ok) {
236
+ return refuseUnstarted(`bounded-exec: ${lastPeak.error}; it is this task's memory charge, so the command does not run\n`);
226
237
  }
238
+ // A task with no recorded peak is charged nothing (null) until it has one.
239
+ const charge = lastPeak?.ok === true ? lastPeak.peakBytes : null;
227
240
  let lease = null;
228
241
  let ordinaryTemp = null;
229
242
  // Before spawn there is nothing to retire. Once started, only the owner's terminal retirement permits deletion.
@@ -363,7 +376,7 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
363
376
  let parentRefusal = null;
364
377
  let parentWait;
365
378
  if (inherited !== null) {
366
- const fromParent = await takeParentCpuTokens(inherited, demand.want);
379
+ const fromParent = await takeParentCpuTokens(inherited, demand.want, charge);
367
380
  if (fromParent.granted) {
368
381
  grant = fromParent;
369
382
  }
@@ -385,7 +398,10 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
385
398
  if (grant !== null) {
386
399
  sized = sizeRunner(demand, requested, grant.tokens);
387
400
  cpu = { tokens: grant.tokens, want: demand.want, waitedMs };
388
- appendStderr(`cowshed: cpu-pool wait done elapsed=${waitedMs}ms status=ok tokens=${grant.tokens}/${demand.want} runner=${demand.kind}\n`);
401
+ appendStderr(`cowshed: cpu-pool wait done elapsed=${waitedMs}ms status=ok tokens=${grant.tokens}/${demand.want} runner=${demand.kind} memory=${charge ?? 'none'}\n`);
402
+ if (grant.oversized !== null) {
403
+ appendStderr(`cowshed: cpu-pool granted while nothing else holds memory: this task's last peak of ${charge} bytes alone exceeds the run's ${grant.oversized.memory} bytes\n`);
404
+ }
389
405
  }
390
406
  else if (leaf?.kind === 'reserve') {
391
407
  sized = { command: sizeRunnerToGrant(leaf.demand, requested), env: {} };
@@ -394,8 +410,11 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
394
410
  sized = sizeRunner(demand, requested, demand.want);
395
411
  }
396
412
  // The run's sub-pool and grant are given back exactly once, however the run ends. The sub-pool closes first, so no
397
- // descendant still draws on tokens the grant has returned.
413
+ // descendant still draws on tokens the grant has returned. A reserved leaf's mbx grant covers the leaf's conclusion,
414
+ // its subtree's retirement included (11_shell.md), so it goes back after `releaseLease`.
398
415
  let pool = null;
416
+ let reservation = null;
417
+ const releaseReservation = () => reservation?.release();
399
418
  let cpuReleased = null;
400
419
  const releaseCpu = () => {
401
420
  cpuReleased ??= (async () => {
@@ -423,7 +442,8 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
423
442
  let unconfirmedAdmission = null;
424
443
  /** When the last successful snapshot completed (`Date.now()`): credit runs to it and no further. */
425
444
  let confirmedAdmissionAt = null;
426
- const queueWait = () => mbxAdmitted || lastQueueRequests.length === 0
445
+ // A reserved leaf's own request on its run's endpoint is its admission wait; a Cargo leaf's comes from mbx's report.
446
+ const queueWait = () => mbxAdmitted || (lastQueueRequests.length === 0 && reservation === null)
427
447
  ? null
428
448
  : {
429
449
  waitedMs: admissionWaitMs(admissionWait, performance.now()),
@@ -439,7 +459,7 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
439
459
  };
440
460
  // Records the run's verdict when it has a task to record as. An `expiry` is set only when a bound ended the run.
441
461
  // Observation and ownership failures remain operational even after valid queue evidence; cancellation alone does not.
442
- const recordRun = async (exitCode, expiry, elapsedMs, operational, noSpace, lockWaits) => {
462
+ const recordRun = async (exitCode, expiry, elapsedMs, operational, noSpace, lockWaits, peakBytes) => {
443
463
  if (context.record === undefined) {
444
464
  return;
445
465
  }
@@ -480,6 +500,7 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
480
500
  verdict,
481
501
  ...(cpu ? { cpu } : {}),
482
502
  ...(parentWait ? { cpuWait: parentWait } : {}),
503
+ peakBytes,
483
504
  });
484
505
  };
485
506
  // A run refused before its command starts says why, gives back its grant and RAM, and records the failure.
@@ -488,16 +509,22 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
488
509
  await releaseCpu();
489
510
  // The command never started, so its lease holds nothing of its own to keep.
490
511
  await releaseLease(false);
491
- await recordRun(1, null, Date.now() - asked, false, null, []);
512
+ await recordRun(1, null, Date.now() - asked, false, null, [], null);
492
513
  return { success: false, terminalOutput: outputChunks.join('') };
493
514
  };
494
515
  if (parentRefusal !== null) {
495
516
  return refuse(`cowshed: cpu-pool parent pool refused: ${parentRefusal}; refusing to run the command\n`);
496
517
  }
518
+ // 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`);
523
+ }
497
524
  // A granted run serves a sub-pool of its own grant, so its descendants draw from that and never from above it.
498
525
  // A pool that cannot be set up refuses the run: no granted command runs beside its grant.
499
526
  if (grant !== null) {
500
- const created = await createCpuPool(grant.tokens, grant.closed);
527
+ const created = await createCpuPool(grant.tokens, grant.closed, null);
501
528
  if (!created.ok) {
502
529
  return refuse(`cowshed: cpu pool unavailable: ${created.error}; refusing to run the command\n`);
503
530
  }
@@ -539,10 +566,22 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
539
566
  : withMbxLeaf(unadmitted, leaf);
540
567
  // Only leaves known to be scheduled can exclude zero-hold time. Others keep lifecycle hang protection.
541
568
  const queued = mbxIdleGated(leaf, sized.command, { ...process.env, ...env });
542
- // fd 4 is unused without a subtree admission, so the grant takes fd 5 either way.
569
+ // fd 4 is unused without a subtree admission, so mbx's descriptor takes fd 5 either way: a Cargo leaf's admission
570
+ // stream, or a reserved leaf's ask for cores and their count (mbx-pool.ts, `MBX_GRANT_FD`).
543
571
  const grantSlots = leaf !== null ? ['ignore', 'pipe'] : [];
544
572
  const rootGrantSlot = leaf !== null ? ['pipe'] : [];
545
573
  const root = owner?.rootArgv(shellCommand);
574
+ // The command's own descriptors past stderr, from fd 3: the lifecycle channel, the subtree admission, mbx's grant.
575
+ const descriptors = root === undefined
576
+ ? hooked === null && leaf === null
577
+ ? []
578
+ : [hooked === null ? 'ignore' : 'pipe', ...grantSlots]
579
+ : [hooked === null ? 'ignore' : 'pipe', 'pipe', ...rootGrantSlot];
580
+ // A run that records its verdict records its command's peak too: the command runs under a waiter that reports it.
581
+ const waiter = context.record === undefined ? null : peakWaiter();
582
+ const measured = waiter?.kind === 'waiter'
583
+ ? underWaiter(waiter.runtime, root?.file ?? '/bin/sh', root?.args ?? ['-c', shellCommand], descriptors)
584
+ : null;
546
585
  let child;
547
586
  const results = directory === null ? null : await createTestResultWriter(directory, { cwd, root: context.root });
548
587
  if (results !== null && !results.ok)
@@ -550,32 +589,48 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
550
589
  resultWriter = results?.ok ? results.writer : null;
551
590
  try {
552
591
  // fd 3 is the runner lifecycle's duplex socket when the command is hooked; the subtree admission takes fd 4, which
553
- // cowshed closes fd 4 before exec; fd 5 carries mbx's admission/release stream, including Cargo descendants.
592
+ // cowshed closes fd 4 before exec; fd 5 is a Cargo leaf's mbx admission stream or a reserved leaf's ask and grant;
593
+ // the waiter, when there is one, passes them on and reports the command's peak on fd 6.
554
594
  child =
555
- root === undefined
556
- ? spawn(shellCommand, [], {
595
+ measured !== null
596
+ ? spawn(measured.file, measured.args, {
557
597
  cwd,
558
598
  env: mergeEnv(lease?.directory ?? ordinaryTemp, env),
559
- shell: true,
560
- stdio: hooked === null && leaf === null
561
- ? 'pipe'
562
- : ['pipe', 'pipe', 'pipe', hooked === null ? 'ignore' : 'pipe', ...grantSlots],
599
+ stdio: measured.stdio,
563
600
  detached: process.platform !== 'win32',
564
601
  windowsHide: true,
565
602
  })
566
- : spawn(root.file, root.args, {
567
- cwd,
568
- env: mergeEnv(lease?.directory ?? ordinaryTemp, env),
569
- stdio: ['pipe', 'pipe', 'pipe', hooked === null ? 'ignore' : 'pipe', 'pipe', ...rootGrantSlot],
570
- detached: process.platform !== 'win32',
571
- windowsHide: true,
572
- });
603
+ : root === undefined
604
+ ? spawn(shellCommand, [], {
605
+ cwd,
606
+ env: mergeEnv(lease?.directory ?? ordinaryTemp, env),
607
+ shell: true,
608
+ stdio: descriptors.length === 0 ? 'pipe' : ['pipe', 'pipe', 'pipe', ...descriptors],
609
+ detached: process.platform !== 'win32',
610
+ windowsHide: true,
611
+ })
612
+ : spawn(root.file, root.args, {
613
+ cwd,
614
+ env: mergeEnv(lease?.directory ?? ordinaryTemp, env),
615
+ stdio: ['pipe', 'pipe', 'pipe', ...descriptors],
616
+ detached: process.platform !== 'win32',
617
+ windowsHide: true,
618
+ });
573
619
  }
574
620
  catch (error) {
575
621
  // Nothing started, so nothing runs under the grant: it goes back here, with the RAM lease.
576
622
  return refuse(`cowshed: could not start the command: ${error instanceof Error ? error.message : String(error)}\n`);
577
623
  }
578
624
  subtreeRetired = false;
625
+ const reportChannel = child.stdio.at(PEAK_FD);
626
+ const waiterReport = measured === null ? null : readWaiterReport(reportChannel instanceof Readable ? reportChannel : null);
627
+ // The leaf's runner is its command: under a waiter, which leads the group, the child the waiter names; else the
628
+ // group's leader itself. A hang snapshot names and samples it (specs/cowshed/08_testing.md).
629
+ const runnerPid = waiterReport?.started ?? Promise.resolve(child.pid ?? null);
630
+ let runner = null;
631
+ void runnerPid.then((pid) => {
632
+ runner = pid;
633
+ });
579
634
  if (root !== undefined) {
580
635
  const channel = child.stdio[HANDLE_FD];
581
636
  const leased = lease;
@@ -631,20 +686,24 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
631
686
  captureTimer = setTimeout(() => {
632
687
  if (state.settled || state.expiry !== null || idleCapture !== capture)
633
688
  return;
634
- const pid = child.pid;
635
- if (pid === undefined) {
689
+ const group = child.pid;
690
+ if (group === undefined) {
636
691
  idleCapture.result = { kind: 'failed', detail: 'runner PID unavailable; no holder conclusion' };
637
692
  return;
638
693
  }
639
694
  idleCapture.result = { kind: 'pending', startedAtMs: Date.now() };
640
- void Promise.resolve()
641
- .then(() => captureDiagnostics({
642
- pid,
643
- pgid: process.platform === 'win32' ? null : pid,
644
- expiresAtMs: capture.expiresAtMs,
645
- deadlineMs: Math.max(0, Math.min(HANG_CAPTURE_MS, capture.expiresAtMs - Date.now())),
646
- signal: capture.controller.signal,
647
- }))
695
+ void runnerPid
696
+ .then((pid) => {
697
+ if (pid === null)
698
+ throw new Error('runner PID unavailable: its waiter named no command; no holder conclusion');
699
+ return captureDiagnostics({
700
+ pid,
701
+ pgid: process.platform === 'win32' ? null : group,
702
+ expiresAtMs: capture.expiresAtMs,
703
+ deadlineMs: Math.max(0, Math.min(HANG_CAPTURE_MS, capture.expiresAtMs - Date.now())),
704
+ signal: capture.controller.signal,
705
+ });
706
+ })
648
707
  .then((snapshot) => {
649
708
  if (idleCapture === capture)
650
709
  idleCapture.result = { kind: 'captured', snapshot };
@@ -681,7 +740,7 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
681
740
  const scheduled = earliestBound(checkingIdle ? { ...bounds, idle: null } : bounds);
682
741
  if (scheduled?.kind !== kind || scheduled.at !== at)
683
742
  return;
684
- if (kind === 'idle' && leaf !== null) {
743
+ if (kind === 'idle' && leaf?.kind === 'cargo') {
685
744
  if (checkingIdle)
686
745
  return;
687
746
  checkingIdle = true;
@@ -738,9 +797,6 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
738
797
  const refreshQueue = async (fresh) => {
739
798
  if (fresh && queueSnapshot !== null)
740
799
  await queueSnapshot;
741
- // A leaf that counts its units before reserving waits in no reservation queue while its fixture builds.
742
- if (counted && !fixtureDone)
743
- return;
744
800
  if (state.settled || state.expiry !== null || queueAbort.signal.aborted || child.pid === undefined)
745
801
  return;
746
802
  const pid = child.pid;
@@ -883,14 +939,15 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
883
939
  clearTimeout(boundTimer);
884
940
  // Preserve explicit live membership before cancellation removes the queued process. A failed bounded observation
885
941
  // leaves the admission unconfirmed, never a manufactured queue or admission.
886
- signalCleanup = (leaf === null ? Promise.resolve() : refreshQueue(false))
942
+ signalCleanup = (leaf?.kind !== 'cargo' ? Promise.resolve() : refreshQueue(false))
887
943
  .finally(() => {
888
944
  stopQueueObservation();
889
945
  return killChildTree();
890
946
  })
891
947
  .finally(releaseCpu)
892
948
  .finally(() => releaseLease(true))
893
- .finally(async () => recordRun(signalToExitCode(signal), state.expiry, Date.now() - startedAt, state.poolCloseError !== null || lifecycleFailure !== null || state.retireError !== null, state.noSpace === null ? null : await state.noSpace, state.lockWaits))
949
+ .finally(releaseReservation)
950
+ .finally(async () => recordRun(signalToExitCode(signal), state.expiry, Date.now() - startedAt, state.poolCloseError !== null || lifecycleFailure !== null || state.retireError !== null, state.noSpace === null ? null : await state.noSpace, state.lockWaits, null))
894
951
  .finally(() => process.kill(process.pid, signal));
895
952
  };
896
953
  const onSigint = () => onTerminationSignal('SIGINT');
@@ -947,7 +1004,7 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
947
1004
  if (bound.kind === 'idle') {
948
1005
  const observed = idleCapture?.result;
949
1006
  state.lockWaits = observed?.kind === 'captured' ? observed.snapshot.lockWaits : [];
950
- appendStderr(`Hang diagnostics: runner pid=${child.pid ?? 'unavailable'} pgid=${process.platform === 'win32' ? 'unavailable' : (child.pid ?? 'unavailable')} expiryAtMs=${Date.now()} scheduledExpiryAtMs=${idleCapture?.expiresAtMs ?? 'unavailable'}\n`);
1007
+ appendStderr(`Hang diagnostics: runner pid=${runner ?? 'unavailable'} pgid=${process.platform === 'win32' ? 'unavailable' : (child.pid ?? 'unavailable')} expiryAtMs=${Date.now()} scheduledExpiryAtMs=${idleCapture?.expiresAtMs ?? 'unavailable'}\n`);
951
1008
  if (observed?.kind === 'captured') {
952
1009
  appendStderr(`Hang diagnostics: outcome=${observed.snapshot.outcome} capturedAtMs=${observed.snapshot.capturedAtMs} actualExpiryOffsetMs=${Date.now() - observed.snapshot.capturedAtMs}\n${observed.snapshot.text}`);
953
1010
  }
@@ -1028,12 +1085,10 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
1028
1085
  fixtureFirst: counted && lifecycle !== null,
1029
1086
  queued,
1030
1087
  });
1031
- // Read every byte after spawn accounting: a chunk can contain several grants and releases. EOF is neither.
1032
1088
  if (leaf !== null) {
1033
1089
  const channel = child.stdio.at(MBX_GRANT_FD);
1034
- if (!(channel instanceof Readable))
1035
- throw new Error('an mbx leaf requires its grant descriptor');
1036
- let said = false;
1090
+ if (!(channel instanceof Duplex))
1091
+ throw new Error('an mbx leaf requires its mbx descriptor');
1037
1092
  const failGrant = (detail) => {
1038
1093
  if (lifecycleFailure !== null || state.expiry !== null || state.settled)
1039
1094
  return;
@@ -1041,34 +1096,82 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
1041
1096
  appendStderr(`bounded-exec: ${lifecycleFailure}\n`);
1042
1097
  termination = terminate();
1043
1098
  };
1044
- channel.on('data', (chunk) => {
1045
- for (const byte of chunk) {
1046
- if (byte === 103) {
1047
- if (!said) {
1048
- mbxAdmitted = true;
1049
- said = true;
1050
- tell(leaf.kind === 'reserve'
1051
- ? `cowshed: mbx reserve granted elapsed=${Date.now() - startedAt}ms priority=${leaf.priority}\n`
1052
- : `cowshed: mbx cargo granted elapsed=${Date.now() - startedAt}ms\n`);
1053
- }
1054
- stepTo({ kind: 'granted', at: Date.now(), testing: isTesting(), fixture: !fixtureDone });
1055
- }
1056
- else if (byte === 114) {
1057
- stepTo({ kind: 'released', at: Date.now() });
1099
+ channel.on('error', (error) => failGrant(error.message));
1100
+ if (reserved !== null) {
1101
+ // The shell asks for its cores in one line and reads the count granted (mbx-pool.ts, `reservation`). A refusal
1102
+ // ends the descriptor unanswered: the shell's read fails, and nothing runs.
1103
+ const { leaf: reservedLeaf, endpoint } = reserved;
1104
+ let asked = '';
1105
+ channel.setEncoding('utf8');
1106
+ channel.on('data', (text) => {
1107
+ if (reservation !== null) {
1108
+ failGrant(`the command asked for cores a second time: ${JSON.stringify(text)}`);
1109
+ return;
1058
1110
  }
1059
- else {
1060
- failGrant(`unexpected byte ${byte}`);
1111
+ asked += text;
1112
+ const end = asked.indexOf('\n');
1113
+ if (end === -1)
1114
+ return;
1115
+ const line = asked.slice(0, end);
1116
+ const cpus = askedCores(line, reservedLeaf.demand.want);
1117
+ if (cpus === null) {
1118
+ failGrant(`the command asked for ${JSON.stringify(line)} cores, not 1 to ${reservedLeaf.demand.want}`);
1119
+ channel.end();
1061
1120
  return;
1062
1121
  }
1063
- }
1064
- });
1065
- channel.on('error', (error) => failGrant(error.message));
1066
- }
1067
- if (leaf !== null) {
1068
- queueTimer = setInterval(() => {
1122
+ admissionWait = stepAdmissionWait(admissionWait, { kind: 'snapshot', at: performance.now(), waiting: true });
1123
+ tell(`cowshed: mbx reserve wait start cpus=${cpus} priority=${reservedLeaf.priority}\n`);
1124
+ const held = reserveCores(endpoint, cpus, reservedLeaf.priority);
1125
+ reservation = held;
1126
+ void held.granted.then((granted) => {
1127
+ if (!granted.ok) {
1128
+ failGrant(`mbx did not grant the reservation: ${granted.error}`);
1129
+ channel.end();
1130
+ return;
1131
+ }
1132
+ // A run that ended while it waited gives the grant back with its CPU (`releaseCpu`).
1133
+ if (state.settled || state.expiry !== null)
1134
+ return;
1135
+ mbxAdmitted = true;
1136
+ tell(`cowshed: mbx reserve granted elapsed=${Date.now() - startedAt}ms priority=${reservedLeaf.priority}\n`);
1137
+ channel.end(`${granted.cpus}\n`);
1138
+ stepTo({ kind: 'granted', at: Date.now(), testing: isTesting(), fixture: !fixtureDone });
1139
+ void granted.lost.then((why) => {
1140
+ if (state.settled || cpuReleased !== null)
1141
+ return;
1142
+ appendStderr(`cowshed: mbx reservation lost while the command ran (${why}); it runs on unheld until it ends\n`);
1143
+ });
1144
+ });
1145
+ });
1146
+ }
1147
+ else {
1148
+ // Read every byte after spawn accounting: a chunk can contain several grants and releases. EOF is neither.
1149
+ let said = false;
1150
+ channel.on('data', (chunk) => {
1151
+ for (const byte of chunk) {
1152
+ if (byte === 103) {
1153
+ if (!said) {
1154
+ mbxAdmitted = true;
1155
+ said = true;
1156
+ tell(`cowshed: mbx cargo granted elapsed=${Date.now() - startedAt}ms\n`);
1157
+ }
1158
+ stepTo({ kind: 'granted', at: Date.now(), testing: isTesting(), fixture: !fixtureDone });
1159
+ }
1160
+ else if (byte === 114) {
1161
+ stepTo({ kind: 'released', at: Date.now() });
1162
+ }
1163
+ else {
1164
+ failGrant(`unexpected byte ${byte}`);
1165
+ return;
1166
+ }
1167
+ }
1168
+ });
1169
+ // Only mbx's activity report says whether a Cargo leaf's compiler or test waits in the pool.
1170
+ queueTimer = setInterval(() => {
1171
+ void refreshQueue(false);
1172
+ }, MBX_QUEUE_POLL_MS);
1069
1173
  void refreshQueue(false);
1070
- }, MBX_QUEUE_POLL_MS);
1071
- void refreshQueue(false);
1174
+ }
1072
1175
  }
1073
1176
  // A grant's connection is a ledger entry, not the command's owner: the lending parent's exit ends it, and what runs
1074
1177
  // under it runs on over budget until it finishes. Its close, while the command runs, is said and changes nothing
@@ -1120,10 +1223,17 @@ export async function runBoundedExec(options, context, killer, tempVolume, inher
1120
1223
  }
1121
1224
  }
1122
1225
  await releaseLease(code !== 0 || state.expiry !== null || state.poolCloseError !== null || lifecycleFailure !== null);
1226
+ releaseReservation();
1227
+ // The command's peak, as its waiter reported it once both ended; a peak that was not measured is said, never zero.
1228
+ const reported = waiterReport === null ? (waiter?.kind === 'unmeasured' ? waiter : null) : await waiterReport.peak;
1229
+ if (reported?.kind === 'unmeasured') {
1230
+ appendStderr(`bounded-exec: this run's memory peak was not measured (${reported.reason}); its record names none\n`);
1231
+ }
1232
+ const peakBytes = reported?.kind === 'measured' ? reported.bytes : null;
1123
1233
  // Broken resource ownership, a lost native lifecycle, or leftovers that could not be retired fail even if a shell
1124
1234
  // branch later exited 0: its verdict is an ordinary operational failure, never a successful or silently unbounded run.
1125
1235
  const operationalFailure = state.poolCloseError !== null || lifecycleFailure !== null || state.retireError !== null;
1126
- await recordRun(code, state.expiry, Date.now() - startedAt, operationalFailure, seen, state.lockWaits);
1236
+ await recordRun(code, state.expiry, Date.now() - startedAt, operationalFailure, seen, state.lockWaits, peakBytes);
1127
1237
  return {
1128
1238
  success: !operationalFailure && resultFailure === null && state.expiry === null && code === 0,
1129
1239
  terminalOutput: outputChunks.join(''),
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The memory facts a run's CPU pool charges by (specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets
3
+ * itself"; 13_telemetry.md, "Job resource peaks"), read by each platform's own mechanism: `host-memory.linux.ts`,
4
+ * `host-memory.darwin.ts`. Callers read them here and never ask which platform they run on.
5
+ */
6
+ export interface HostMemory {
7
+ /** The memory the run's pool charges its leaves' peaks against. */
8
+ readRunMemory(): Promise<RunMemoryReading>;
9
+ /**
10
+ * A leaf's peak in bytes, from the `maxRSS` that Bun's `Subprocess.resourceUsage()` on `bunVersion` reports for
11
+ * it: the exited leaf's `ru_maxrss`.
12
+ */
13
+ peakBytes(maxRss: number, bunVersion: string): number;
14
+ }
15
+ /** The run's memory in bytes, and where it was read: the file or sysctl that holds it. */
16
+ export interface RunMemory {
17
+ readonly bytes: number;
18
+ readonly source: string;
19
+ }
20
+ /** Why the run's memory could not be read: the file or sysctl, and what reading it gave. */
21
+ export interface RunMemoryError {
22
+ readonly operation: 'read-run-memory';
23
+ /**
24
+ * `unreadable`: reading it failed; `malformed`: it held no byte count; `unmounted`: the run's cgroup v2 hierarchy is
25
+ * mounted nowhere this process can see; `unsupported-platform`: the platform has no reader.
26
+ */
27
+ readonly code: 'unreadable' | 'malformed' | 'unmounted' | 'unsupported-platform';
28
+ readonly subject: string;
29
+ readonly cause: string;
30
+ }
31
+ export type RunMemoryReading = {
32
+ readonly ok: true;
33
+ readonly memory: RunMemory;
34
+ } | {
35
+ readonly ok: false;
36
+ readonly error: RunMemoryError;
37
+ };
38
+ /** A run whose memory cannot be read does not start: it is not shown to have room. */
39
+ export declare class RunMemoryUnreadable extends Error {
40
+ readonly error: RunMemoryError;
41
+ constructor(error: RunMemoryError);
42
+ }
43
+ /** This platform's mechanisms, or null where it has none: there a leaf's peak goes unmeasured. */
44
+ export declare const HOST_MEMORY: HostMemory | null;
45
+ /** The run's memory on this platform, or why it cannot be read. */
46
+ export declare function readRunMemory(): Promise<RunMemoryReading>;
47
+ //# sourceMappingURL=host-memory.d.ts.map
@@ -0,0 +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,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"}
@@ -0,0 +1,3 @@
1
+ import type { HostMemory } from './host-memory.js';
2
+ export declare const darwinMemory: HostMemory;
3
+ //# sourceMappingURL=host-memory.darwin.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,40 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { promisify } from 'node:util';
3
+ /**
4
+ * macOS's memory mechanisms. A run's memory is the host's, `hw.memsize`: macOS confines no job to a memory limit of
5
+ * its own. Bun reports a leaf's `ru_maxrss` in bytes, as macOS does.
6
+ */
7
+ const run = promisify(execFile);
8
+ const HW_MEMSIZE = 'hw.memsize';
9
+ async function readRunMemory() {
10
+ let stdout;
11
+ try {
12
+ ({ stdout } = await run('sysctl', ['-n', HW_MEMSIZE]));
13
+ }
14
+ catch (error) {
15
+ return {
16
+ ok: false,
17
+ error: {
18
+ operation: 'read-run-memory',
19
+ code: 'unreadable',
20
+ subject: HW_MEMSIZE,
21
+ cause: error instanceof Error ? error.message : String(error),
22
+ },
23
+ };
24
+ }
25
+ const value = stdout.trim();
26
+ const bytes = Number(value);
27
+ if (!/^\d+$/.test(value) || !Number.isSafeInteger(bytes) || bytes === 0) {
28
+ return {
29
+ ok: false,
30
+ error: {
31
+ operation: 'read-run-memory',
32
+ code: 'malformed',
33
+ subject: HW_MEMSIZE,
34
+ cause: `sysctl printed ${JSON.stringify(value)}, not a byte count`,
35
+ },
36
+ };
37
+ }
38
+ return { ok: true, memory: { bytes, source: HW_MEMSIZE } };
39
+ }
40
+ export const darwinMemory = { readRunMemory, peakBytes: (maxRss) => maxRss };
@@ -0,0 +1,29 @@
1
+ import { darwinMemory } from './host-memory.darwin.js';
2
+ import { linuxMemory } from './host-memory.linux.js';
3
+ /** A run whose memory cannot be read does not start: it is not shown to have room. */
4
+ export class RunMemoryUnreadable extends Error {
5
+ error;
6
+ constructor(error) {
7
+ super(`bounded-exec: this run's memory cannot be read (${error.operation} ${error.code} ${error.subject}: ${error.cause}); a run whose memory cannot be read is not shown to have room, so it does not start`);
8
+ this.error = error;
9
+ this.name = 'RunMemoryUnreadable';
10
+ }
11
+ }
12
+ const PLATFORMS = { darwin: darwinMemory, linux: linuxMemory };
13
+ /** This platform's mechanisms, or null where it has none: there a leaf's peak goes unmeasured. */
14
+ export const HOST_MEMORY = PLATFORMS[process.platform] ?? null;
15
+ /** The run's memory on this platform, or why it cannot be read. */
16
+ export function readRunMemory() {
17
+ if (HOST_MEMORY === null) {
18
+ return Promise.resolve({
19
+ ok: false,
20
+ error: {
21
+ operation: 'read-run-memory',
22
+ code: 'unsupported-platform',
23
+ subject: process.platform,
24
+ cause: 'only Linux and macOS have a reader of the run memory',
25
+ },
26
+ });
27
+ }
28
+ return HOST_MEMORY.readRunMemory();
29
+ }
@@ -0,0 +1,14 @@
1
+ import type { HostMemory, RunMemoryReading } from './host-memory.js';
2
+ /** A file's text, or the errno code and message its read failed with. */
3
+ export type FileRead = {
4
+ readonly ok: true;
5
+ readonly text: string;
6
+ } | {
7
+ readonly ok: false;
8
+ readonly code: string;
9
+ readonly message: string;
10
+ };
11
+ /** The run's memory, reading every file through `read`. */
12
+ export declare function linuxRunMemory(read: (path: string) => Promise<FileRead>): Promise<RunMemoryReading>;
13
+ export declare const linuxMemory: HostMemory;
14
+ //# sourceMappingURL=host-memory.linux.d.ts.map