@smoothbricks/nx-plugin 0.7.11 → 0.7.13

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 (108) hide show
  1. package/README.md +87 -25
  2. package/dist/.build-inputs.json +25 -17
  3. package/dist/built-output-packages.d.ts +27 -0
  4. package/dist/built-output-packages.d.ts.map +1 -0
  5. package/dist/built-output-packages.js +97 -0
  6. package/dist/cross-check-policy.d.ts +13 -1
  7. package/dist/cross-check-policy.d.ts.map +1 -1
  8. package/dist/cross-check-policy.js +13 -1
  9. package/dist/executors/bounded-exec/admission-queue.d.ts +68 -0
  10. package/dist/executors/bounded-exec/admission-queue.d.ts.map +1 -0
  11. package/dist/executors/bounded-exec/admission-queue.js +131 -0
  12. package/dist/executors/bounded-exec/cpu-grant.d.ts +14 -0
  13. package/dist/executors/bounded-exec/cpu-grant.d.ts.map +1 -0
  14. package/dist/executors/bounded-exec/cpu-grant.js +18 -0
  15. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts +5 -4
  16. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts.map +1 -1
  17. package/dist/executors/bounded-exec/cpu-pool.d.ts +26 -24
  18. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  19. package/dist/executors/bounded-exec/cpu-pool.js +88 -72
  20. package/dist/executors/bounded-exec/executor.d.ts +5 -7
  21. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  22. package/dist/executors/bounded-exec/executor.js +69 -133
  23. package/dist/executors/bounded-exec/host-memory.d.ts +22 -0
  24. package/dist/executors/bounded-exec/host-memory.d.ts.map +1 -1
  25. package/dist/executors/bounded-exec/host-memory.darwin.d.ts.map +1 -1
  26. package/dist/executors/bounded-exec/host-memory.darwin.js +7 -1
  27. package/dist/executors/bounded-exec/host-memory.js +6 -0
  28. package/dist/executors/bounded-exec/host-memory.linux.d.ts +3 -1
  29. package/dist/executors/bounded-exec/host-memory.linux.d.ts.map +1 -1
  30. package/dist/executors/bounded-exec/host-memory.linux.js +43 -1
  31. package/dist/executors/bounded-exec/memory-retry.d.ts +39 -0
  32. package/dist/executors/bounded-exec/memory-retry.d.ts.map +1 -0
  33. package/dist/executors/bounded-exec/memory-retry.js +33 -0
  34. package/dist/executors/bounded-exec/parent-grant.d.ts +43 -0
  35. package/dist/executors/bounded-exec/parent-grant.d.ts.map +1 -0
  36. package/dist/executors/bounded-exec/parent-grant.js +37 -0
  37. package/dist/executors/bounded-exec/peak-charge.d.ts +17 -0
  38. package/dist/executors/bounded-exec/peak-charge.d.ts.map +1 -0
  39. package/dist/executors/bounded-exec/peak-charge.js +20 -0
  40. package/dist/executors/bounded-exec/pool-admission.d.ts +31 -7
  41. package/dist/executors/bounded-exec/pool-admission.d.ts.map +1 -1
  42. package/dist/executors/bounded-exec/pool-admission.js +44 -8
  43. package/dist/executors/bounded-exec/pool-environment.d.ts +25 -0
  44. package/dist/executors/bounded-exec/pool-environment.d.ts.map +1 -0
  45. package/dist/executors/bounded-exec/pool-environment.js +34 -0
  46. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  47. package/dist/executors/bounded-exec/run-cpu-pool.js +2 -2
  48. package/dist/executors/bounded-exec/verdict.d.ts +5 -18
  49. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  50. package/dist/executors/bounded-exec/verdict.js +12 -16
  51. package/dist/index.d.ts +4 -1
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +62 -21
  54. package/dist/managed-files/ci-workflow.d.ts +29 -5
  55. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  56. package/dist/managed-files/ci-workflow.js +111 -16
  57. package/dist/managed-files/context.d.ts.map +1 -1
  58. package/dist/managed-files/context.js +1 -0
  59. package/dist/managed-files/files.d.ts +3 -1
  60. package/dist/managed-files/files.d.ts.map +1 -1
  61. package/dist/managed-files/files.js +2 -0
  62. package/dist/managed-files/publish-workflow.d.ts +6 -1
  63. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  64. package/dist/managed-files/publish-workflow.js +16 -11
  65. package/dist/owned-installation.d.ts.map +1 -1
  66. package/dist/owned-installation.js +48 -14
  67. package/dist/typecheck-route.d.ts +28 -0
  68. package/dist/typecheck-route.d.ts.map +1 -0
  69. package/dist/typecheck-route.js +109 -0
  70. package/dist/workspace-manifest.d.ts +50 -5
  71. package/dist/workspace-manifest.d.ts.map +1 -1
  72. package/executors.json +10 -0
  73. package/managed/raw/patches/nx@24.0.3.patch +16 -0
  74. package/managed/raw/patches/ttsc@0.34.1.patch +93 -8
  75. package/managed/raw/tooling/direnv/apple-developer.sh +8 -1
  76. package/managed/raw/tooling/direnv/compiler-variables.sh +1 -1
  77. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +4 -2
  78. package/managed/raw/tooling/direnv/secret-references.ts +19 -5
  79. package/managed/raw/tooling/git-hooks/pre-push.sh +20 -10
  80. package/managed/templates/github/actions/forgejo-artifact.cjs +9 -9
  81. package/managed/templates/github/actions/setup-devenv/action.yml +53 -0
  82. package/package.json +2 -2
  83. package/src/built-output-packages.ts +125 -0
  84. package/src/cross-check-policy.ts +14 -1
  85. package/src/executors/bounded-exec/admission-queue.ts +163 -0
  86. package/src/executors/bounded-exec/cpu-grant.ts +32 -0
  87. package/src/executors/bounded-exec/cpu-pool-thread.ts +5 -4
  88. package/src/executors/bounded-exec/cpu-pool.ts +123 -58
  89. package/src/executors/bounded-exec/executor.ts +93 -168
  90. package/src/executors/bounded-exec/host-memory.darwin.ts +7 -1
  91. package/src/executors/bounded-exec/host-memory.linux.ts +42 -2
  92. package/src/executors/bounded-exec/host-memory.ts +25 -0
  93. package/src/executors/bounded-exec/memory-retry.ts +63 -0
  94. package/src/executors/bounded-exec/parent-grant.ts +75 -0
  95. package/src/executors/bounded-exec/peak-charge.ts +25 -0
  96. package/src/executors/bounded-exec/pool-admission.ts +66 -11
  97. package/src/executors/bounded-exec/pool-environment.ts +45 -0
  98. package/src/executors/bounded-exec/run-cpu-pool.ts +5 -4
  99. package/src/executors/bounded-exec/verdict.ts +17 -25
  100. package/src/index.ts +70 -21
  101. package/src/managed-files/ci-workflow.ts +143 -16
  102. package/src/managed-files/context.ts +1 -0
  103. package/src/managed-files/files.ts +5 -0
  104. package/src/managed-files/publish-workflow.ts +26 -10
  105. package/src/owned-installation.ts +63 -14
  106. package/src/typecheck-route.ts +64 -0
  107. package/src/workspace-manifest.ts +56 -5
  108. package/test-timings.json +141 -137
@@ -0,0 +1,131 @@
1
+ import { admissionWaitMs, NO_ADMISSION_WAIT, stepAdmissionWait } from './bounds.js';
2
+ import { HANG_CAPTURE_MS } from './hang-diagnostics.js';
3
+ import { MBX_QUEUE_POLL_MS, snapshotMbxQueue } from './mbx-queue.js';
4
+ import { describeAdmissionQueue } from './verdict.js';
5
+ /** One run's view of the admission queue its leaf waits in. */
6
+ export class AdmissionObservation {
7
+ host;
8
+ /** mbx granted the leaf its cores. */
9
+ admitted = false;
10
+ wait = NO_ADMISSION_WAIT;
11
+ requests = [];
12
+ /**
13
+ * Why the last admission snapshot failed, while the one after it has not succeeded; null otherwise. A snapshot only
14
+ * decides whether waiting on mbx admission is credited against the idle bound, so a failed one withholds that credit
15
+ * and ends nothing: the bounds decide, and an idle expiry names it.
16
+ */
17
+ failure = null;
18
+ /** When the last successful snapshot completed (`Date.now()`): credit runs to it and no further. */
19
+ confirmedAt = null;
20
+ abort = new AbortController();
21
+ timer;
22
+ snapshot = null;
23
+ lastDiagnostic = '';
24
+ lastDiagnosticAt = null;
25
+ lastSnapshotDiagnostics = '';
26
+ constructor(host) {
27
+ this.host = host;
28
+ }
29
+ /** Why the last snapshot failed, while the one after it has not succeeded. */
30
+ get unconfirmed() {
31
+ return this.failure;
32
+ }
33
+ /**
34
+ * A reserved leaf's own request on its run's endpoint is its admission wait; a Cargo leaf's comes from mbx's report.
35
+ * `reserved` is whether the leaf holds a reservation of its own.
36
+ */
37
+ queueWait(reserved) {
38
+ return this.admitted || (this.requests.length === 0 && !reserved)
39
+ ? null
40
+ : { waitedMs: admissionWaitMs(this.wait, performance.now()), requests: this.requests };
41
+ }
42
+ /** A reserved leaf's request began to wait on its run's endpoint. */
43
+ waiting() {
44
+ this.wait = stepAdmissionWait(this.wait, { kind: 'snapshot', at: performance.now(), waiting: true });
45
+ }
46
+ /** Only mbx's activity report says whether a Cargo leaf's compiler or test waits in the pool: poll it. */
47
+ poll() {
48
+ this.timer = setInterval(() => {
49
+ void this.refresh(false);
50
+ }, MBX_QUEUE_POLL_MS);
51
+ void this.refresh(false);
52
+ }
53
+ /** The run ended: the wait stops counting and no snapshot is taken or credited again. */
54
+ stop() {
55
+ this.wait = stepAdmissionWait(this.wait, { kind: 'stop', at: performance.now() });
56
+ clearInterval(this.timer);
57
+ this.abort.abort();
58
+ }
59
+ /** The snapshot in flight, if any, has finished. */
60
+ async idle() {
61
+ await this.snapshot;
62
+ }
63
+ unconfirm(detail) {
64
+ if (this.host.over() || this.abort.signal.aborted)
65
+ return;
66
+ if (this.failure !== detail) {
67
+ this.host.tell(`bounded-exec: mbx admission snapshot failed: ${detail}; admission is unconfirmed, so this wait earns no idle credit until a snapshot succeeds\n`);
68
+ }
69
+ this.failure = detail;
70
+ this.requests = [];
71
+ this.wait = stepAdmissionWait(this.wait, { kind: 'snapshot', at: performance.now(), waiting: false });
72
+ // The wait since the last confirmed snapshot is unconfirmed: the idle bound resumes from that snapshot.
73
+ this.host.step({
74
+ kind: 'admission-queue',
75
+ at: this.confirmedAt ?? Date.now(),
76
+ waiting: false,
77
+ testing: this.host.testing(),
78
+ });
79
+ }
80
+ /** Take a snapshot; `fresh` drains an older poll first. */
81
+ async refresh(fresh) {
82
+ if (fresh && this.snapshot !== null)
83
+ await this.snapshot;
84
+ const pid = this.host.pid();
85
+ if (this.host.over() || this.abort.signal.aborted || pid === undefined)
86
+ return;
87
+ this.snapshot ??= (async () => {
88
+ const snapshot = await snapshotMbxQueue(pid, this.host.environment(), this.abort.signal, this.host.queueHost);
89
+ if (this.host.over() || this.abort.signal.aborted)
90
+ return;
91
+ if (!snapshot.ok) {
92
+ this.unconfirm(snapshot.detail);
93
+ return;
94
+ }
95
+ const diagnostics = snapshot.diagnostics?.join('; ') ?? '';
96
+ if (diagnostics !== '' && diagnostics !== this.lastSnapshotDiagnostics) {
97
+ this.host.tell(`bounded-exec: ${diagnostics}\n`);
98
+ }
99
+ this.lastSnapshotDiagnostics = diagnostics;
100
+ const waiting = snapshot.requests.length > 0;
101
+ const observedAt = Date.now();
102
+ const observedWaitAt = performance.now();
103
+ this.wait = stepAdmissionWait(this.wait, { kind: 'snapshot', at: observedWaitAt, waiting });
104
+ this.failure = null;
105
+ this.confirmedAt = observedAt;
106
+ if (waiting) {
107
+ this.requests = snapshot.requests;
108
+ const positions = JSON.stringify(snapshot.requests);
109
+ if (positions !== this.lastDiagnostic ||
110
+ this.lastDiagnosticAt === null ||
111
+ Date.now() - this.lastDiagnosticAt >= HANG_CAPTURE_MS) {
112
+ const waitedMs = admissionWaitMs(this.wait, observedWaitAt);
113
+ this.host.tell(`cowshed: mbx ${describeAdmissionQueue({ waitedMs, requests: snapshot.requests })}\n`);
114
+ this.lastDiagnostic = positions;
115
+ this.lastDiagnosticAt = Date.now();
116
+ }
117
+ }
118
+ else {
119
+ this.lastDiagnostic = '';
120
+ }
121
+ this.host.step({ kind: 'admission-queue', at: observedAt, waiting, testing: this.host.testing() });
122
+ })()
123
+ .catch((error) => {
124
+ this.unconfirm(error instanceof Error ? error.message : String(error));
125
+ })
126
+ .finally(() => {
127
+ this.snapshot = null;
128
+ });
129
+ await this.snapshot;
130
+ }
131
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The CPU grant a run's record names (verdict.ts keeps the record; this is what it says of the pool). The tokens are the
3
+ * parallelism the record's peak was measured under, which the task's next run reads (peak-charge.ts) to charge that
4
+ * peak in proportion to the parallelism it is lent. It is scheduling metadata: a record that names none, or names one
5
+ * that is malformed, changes no verdict a passing run records.
6
+ */
7
+ export interface CpuGrantRecord {
8
+ readonly tokens: number;
9
+ readonly want: number;
10
+ readonly waitedMs: number;
11
+ }
12
+ /** What a record's `cpu` field holds: no grant, a grant, or something that is no grant. */
13
+ export declare function parseCpuGrant(value: unknown): CpuGrantRecord | undefined | 'malformed';
14
+ //# sourceMappingURL=cpu-grant.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cpu-grant.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/cpu-grant.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED,2FAA2F;AAC3F,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,cAAc,GAAG,SAAS,GAAG,WAAW,CAkBtF"}
@@ -0,0 +1,18 @@
1
+ /** What a record's `cpu` field holds: no grant, a grant, or something that is no grant. */
2
+ export function parseCpuGrant(value) {
3
+ if (value === undefined) {
4
+ return undefined;
5
+ }
6
+ if (typeof value !== 'object' || value === null) {
7
+ return 'malformed';
8
+ }
9
+ const tokens = Reflect.get(value, 'tokens');
10
+ const want = Reflect.get(value, 'want');
11
+ const waitedMs = Reflect.get(value, 'waitedMs');
12
+ if (!(typeof tokens === 'number' && Number.isSafeInteger(tokens) && tokens >= 1) ||
13
+ !(typeof want === 'number' && Number.isSafeInteger(want) && want >= 1) ||
14
+ !(typeof waitedMs === 'number' && Number.isFinite(waitedMs) && waitedMs >= 0)) {
15
+ return 'malformed';
16
+ }
17
+ return { tokens, want, waitedMs };
18
+ }
@@ -1,4 +1,5 @@
1
1
  import { type CloseOutcome } from './cpu-pool.js';
2
+ import type { RunMemory } from './host-memory.js';
2
3
  /**
3
4
  * The thread an Nx run's CPU pool runs on (`run-cpu-pool.ts`; specs/cowshed/05_gateway.md, "Every Nx run budgets
4
5
  * itself"). The process that serves a run's pool, the Nx daemon when installed plugins load in it or the Nx CLI without
@@ -7,13 +8,13 @@ import { type CloseOutcome } from './cpu-pool.js';
7
8
  * jobserver fifo and heartbeat share an event loop that runs nothing else, so the heartbeat proves that the actor which
8
9
  * admits and grants is turning.
9
10
  *
10
- * The thread is started with the pool's count and the run's memory. It says once that the pool serves, with the environment a task inherits,
11
- * or why it cannot; the host's one message is its close, after which the thread says how the pool closed.
11
+ * The thread is started with the pool's count and the run's memory. It says once that the pool serves, with the environment a
12
+ * task inherits, or why it cannot; the host's one message is its close, after which the thread says how the pool closed.
12
13
  */
13
- /** What the thread is started with: the run's count, and the run's memory in bytes its grants are charged against. */
14
+ /** What the thread is started with: the run's count, and the run's memory its grants are charged against. */
14
15
  export interface CpuPoolThreadData {
15
16
  readonly tokens: number;
16
- readonly memory: number;
17
+ readonly memory: RunMemory;
17
18
  }
18
19
  /** What the thread says, in order: the pool serves or failed; after the host's close, how the pool closed. */
19
20
  export type CpuPoolThreadReport = {
@@ -1 +1 @@
1
- {"version":3,"file":"cpu-pool-thread.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/cpu-pool-thread.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,YAAY,EAAiB,MAAM,eAAe,CAAC;AAEjE;;;;;;;;;;GAUG;AAEH,sHAAsH;AACtH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,8GAA8G;AAC9G,MAAM,MAAM,mBAAmB,GAC3B;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,GAC1E;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACnD;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAA;CAAE,CAAC"}
1
+ {"version":3,"file":"cpu-pool-thread.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/cpu-pool-thread.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,YAAY,EAAiB,MAAM,eAAe,CAAC;AACjE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;GAUG;AAEH,6GAA6G;AAC7G,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;CAC5B;AAED,8GAA8G;AAC9G,MAAM,MAAM,mBAAmB,GAC3B;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,GAC1E;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACnD;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAA;CAAE,CAAC"}
@@ -1,5 +1,7 @@
1
1
  import typia from 'typia';
2
2
  import { type Unleased } from './gateway-lease.js';
3
+ import { type LiveUsageReading, type RunMemory } from './host-memory.js';
4
+ import { type InheritedCpuPool } from './pool-environment.js';
3
5
  /**
4
6
  * A CPU pool: the tokens a process lends the runners it starts, held live rather than inherited as a
5
7
  * bare count (specs/cowshed/05_gateway.md, "Host scheduling", "Parallelism inside a job"). An Nx run
@@ -25,14 +27,6 @@ import { type Unleased } from './gateway-lease.js';
25
27
  * which the holder is not using while it waits on the child it lends it to; release writes the bytes back and
26
28
  * frees the slot. A Cargo reading the fifo and a nested runner holding a socket grant so share one count.
27
29
  */
28
- /** The environment name a child reads its parent's pool endpoint from; its count is `CPU_TOKENS_ENV`. */
29
- export declare const CPU_POOL_ENV = "BOUNDED_EXEC_CPU_POOL";
30
- /** An inherited pool descriptor; only a grant from its endpoint proves a live allocation. */
31
- export interface InheritedCpuPool {
32
- readonly socket: string;
33
- /** The count the parent names beside its endpoint. */
34
- readonly tokens: number;
35
- }
36
30
  /** A running pool: the environment a child inherits to reach it, and how to close it. */
37
31
  export interface CpuPool {
38
32
  /** The endpoint, the count the pool actually holds, and its make jobserver fifo. */
@@ -55,38 +49,46 @@ export interface Oversized {
55
49
  readonly memory: number & typia.tags.Type<'uint64'>;
56
50
  }
57
51
  /**
58
- * The inherited descriptor, or null when malformed or absent. A missing or bare count, or an empty
59
- * or NUL-bearing endpoint, proves no allocation; the count is a positive whole number. A valid
60
- * descriptor still needs a live endpoint grant.
61
- */
62
- export declare function readInheritedCpuPool(environment: Readonly<NodeJS.ProcessEnv>): InheritedCpuPool | null;
63
- /**
64
- * Tokens a parent pool lent, held until released, or why there are none. `oversized` names the run's memory when the
65
- * child's charge alone exceeds it and it was granted because nothing else held memory. `closed` resolves when the
66
- * grant's connection closes, however it closes, so a sub-pool built on the grant ends with it.
52
+ * Tokens a parent pool lent, held until released, or why there are none. `charged` is the bytes the pool charged the
53
+ * grant, null from a pool that charges none. `oversized` names the run's memory when the child's charge alone exceeds
54
+ * it and it was granted because nothing else held memory. `closed` resolves when the grant's connection closes, however
55
+ * it closes, so a sub-pool built on the grant ends with it.
67
56
  */
68
57
  export type TokenGrant = {
69
58
  granted: true;
70
59
  tokens: number;
60
+ charged: number | null;
71
61
  oversized: Oversized | null;
72
62
  release: () => void;
73
63
  closed: Promise<void>;
74
64
  } | Unleased;
65
+ /** What a leaf tells the pool beyond its count and its charge. */
66
+ export interface AskContext {
67
+ /** The tokens the task was lent when its recorded peak was measured. */
68
+ readonly parallelism?: number | undefined;
69
+ /** The Nx task the leaf runs, named in the pool's line for the grant. */
70
+ readonly task?: string | undefined;
71
+ /** The kernel killed the command for memory: it is run again, granted only while nothing else holds a grant. */
72
+ readonly alone?: boolean | undefined;
73
+ }
75
74
  /**
76
75
  * Ask the immediate parent pool for `want` tokens, charged `memory` bytes: the task's last recorded peak, or null when
77
- * it has none. The inherited count does not bound the ask: a pool grants at most what it holds. A granted lease is the
78
- * child's local allocation: its connection is the grant, and closing it returns only that allocation. An absent
79
- * endpoint grants nothing and refuses nothing; a connected endpoint's refusal or protocol failure must fail admission
80
- * rather than run the child beside its parent's count.
76
+ * it has none. `context.parallelism` is the tokens the task was lent when that peak was measured; the pool charges the
77
+ * peak in proportion to the tokens it lends now when that is more, and lends fewer when the run's memory holds no more.
78
+ * The inherited count does not bound the ask: a pool grants at most what it holds. A granted lease is the child's local
79
+ * allocation: its connection is the grant, and closing it returns only that allocation. An absent endpoint grants
80
+ * nothing and refuses nothing; a connected endpoint's refusal or protocol failure must fail admission rather than run
81
+ * the child beside its parent's count.
81
82
  */
82
- export declare function takeParentCpuTokens(parent: InheritedCpuPool, want: number, memory: number | null): Promise<TokenGrant>;
83
+ export declare function takeParentCpuTokens(parent: InheritedCpuPool, want: number, memory: number | null, context?: AskContext): Promise<TokenGrant>;
83
84
  /**
84
85
  * Make a pool of `tokens` that lives while `closed` does: when `closed` resolves the pool refuses
85
86
  * grants, ends its child connections, drains its jobserver fifo and removes its directory. A run's own
86
- * pool charges its grants against `memory`, the run's memory in bytes; a sub-pool charges none (null).
87
+ * pool charges its grants against `memory` and says each grant with what the run's bound holds live
88
+ * (`liveUsage`); a sub-pool charges none (null).
87
89
  * Setup failures are values; a non-positive or unsafe count is a programmer bug and throws.
88
90
  */
89
- export declare function createCpuPool(tokens: number, closed: Promise<void>, memory: number | null): Promise<{
91
+ export declare function createCpuPool(tokens: number, closed: Promise<void>, memory: RunMemory | null, liveUsage?: (bound: string) => LiveUsageReading): Promise<{
90
92
  ok: true;
91
93
  pool: CpuPool;
92
94
  } | {
@@ -1 +1 @@
1
- {"version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AC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CAuXrE"}
@@ -1,5 +1,6 @@
1
1
  import * as _isTypeUint32_1 from "typia/lib/internal/_isTypeUint32";
2
2
  import * as _isTypeUint64_1 from "typia/lib/internal/_isTypeUint64";
3
+ import * as _stringLengthLte_1 from "typia/lib/internal/_stringLengthLte";
3
4
  import * as _jsonStringifyNumber_1 from "typia/lib/internal/_jsonStringifyNumber";
4
5
  import * as _jsonStringifyTail_1 from "typia/lib/internal/_jsonStringifyTail";
5
6
  import * as _jsonStringifyString_1 from "typia/lib/internal/_jsonStringifyString";
@@ -13,40 +14,14 @@ import { promisify } from 'node:util';
13
14
  import typia from 'typia';
14
15
  import { ownedSocketDirectory } from '../../testing.js';
15
16
  import { holdLease } from './gateway-lease.js';
17
+ import { readLiveUsage } from './host-memory.js';
16
18
  import { CpuLeaseWait, HEARTBEAT_MS } from './lease-wait.js';
17
19
  import { admit } from './pool-admission.js';
18
- import { CPU_TOKENS_ENV, parseInheritedTokens } from './runner-sizing.js';
19
- /**
20
- * A CPU pool: the tokens a process lends the runners it starts, held live rather than inherited as a
21
- * bare count (specs/cowshed/05_gateway.md, "Host scheduling", "Parallelism inside a job"). An Nx run
22
- * serves one on a thread of its own (`run-cpu-pool.ts`), and every granted bounded-exec serves a
23
- * sub-pool of its grant to its own descendants, so the runners of one build tree together stay
24
- * within the tree's count.
25
- *
26
- * The pool is a private Unix socket in a directory the process owns: the directory 0700, the socket
27
- * 0600. A child asks it for a count with one request line, in `holdLease`'s framing. The pool
28
- * answers `queued` at once and `granted` when the child reaches the head of its FIFO queue. A grant
29
- * lasts while its connection does, so a child that dies returns its tokens with its socket. A pool
30
- * grants only from its own count, and a pool whose upstream grant has gone grants nothing.
31
- *
32
- * A run's own pool also charges memory (`pool-admission.ts`; specs/cowshed/05_gateway.md, "Without mbx, every Nx run
33
- * budgets itself"). A request names its task's last recorded peak as `memory`, in bytes, and is granted only while
34
- * the charges of the leaves holding grants and its own fit the run's memory; a request that names none is charged
35
- * nothing, as mbx's omitted `--memory` is. A sub-pool charges no memory: its holder's charge covers its tree.
36
- *
37
- * The same count is a GNU make jobserver fifo beside the socket, mode 0600, which Cargo, its build
38
- * scripts and every nested `cargo` read through `CARGO_MAKEFLAGS`/`MAKEFLAGS` (specs/cowshed/05_gateway.md,
39
- * "Cargo reads its holder's jobserver"). A pool of `g` holds `g − 1` token bytes there; the holder's own
40
- * command holds the implicit first token. A socket grant takes fifo bytes first and that implicit slot last,
41
- * which the holder is not using while it waits on the child it lends it to; release writes the bytes back and
42
- * frees the slot. A Cargo reading the fifo and a nested runner holding a socket grant so share one count.
43
- */
44
- /** The environment name a child reads its parent's pool endpoint from; its count is `CPU_TOKENS_ENV`. */
45
- export const CPU_POOL_ENV = 'BOUNDED_EXEC_CPU_POOL';
20
+ import { poolEnvironment } from './pool-environment.js';
46
21
  /** Exactly a request's fields: any other field is an invalid request. */
47
22
  const isRequest = (() => {
48
- const _io0 = (input, _exceptionable = true) => "cpu-pool" === input.op && ("number" === typeof input.want && (_isTypeUint32_1._isTypeUint32(input.want) && 1 <= input.want)) && (undefined === input.memory || "number" === typeof input.memory && _isTypeUint64_1._isTypeUint64(input.memory)) && (2 === Object.keys(input).length || Object.keys(input).every(key => {
49
- if (["op", "want", "memory"].some(prop => key === prop))
23
+ const _io0 = (input, _exceptionable = true) => "cpu-pool" === input.op && ("number" === typeof input.want && (_isTypeUint32_1._isTypeUint32(input.want) && 1 <= input.want)) && (undefined === input.memory || "number" === typeof input.memory && _isTypeUint64_1._isTypeUint64(input.memory)) && (undefined === input.parallelism || "number" === typeof input.parallelism && (_isTypeUint32_1._isTypeUint32(input.parallelism) && 1 <= input.parallelism)) && (undefined === input.task || "string" === typeof input.task && _stringLengthLte_1._stringLengthLte(input.task, 256)) && (undefined === input.alone || "boolean" === typeof input.alone) && (2 === Object.keys(input).length || Object.keys(input).every(key => {
24
+ if (["op", "want", "memory", "parallelism", "task", "alone"].some(prop => key === prop))
50
25
  return true;
51
26
  const value = input[key];
52
27
  if (undefined === value)
@@ -59,7 +34,7 @@ const stringifyReply = (() => {
59
34
  const _ip0 = input => true === input["ok"];
60
35
  const _so0 = input => `{"ok":${String(input.ok)},"progress":${String(_jsonStringifyNumber_1._jsonStringifyNumber(input.progress))}}`;
61
36
  const _so1 = input => `{"ok":${String(input.ok)},"lease":${"\"" + input.lease + "\""},"server":${String(_jsonStringifyNumber_1._jsonStringifyNumber(input.server))}}`;
62
- const _so2 = input => `{${_jsonStringifyTail_1._jsonStringifyTail(`"ok":${String(input.ok)},"lease":${"\"" + input.lease + "\""},"tokens":${String(_jsonStringifyNumber_1._jsonStringifyNumber(input.tokens))},${undefined === input.oversized ? "" : `"oversized":${undefined !== input.oversized ? _so3(input.oversized) : undefined}`}`)}}`;
37
+ const _so2 = input => `{${_jsonStringifyTail_1._jsonStringifyTail(`"ok":${String(input.ok)},"lease":${"\"" + input.lease + "\""},"tokens":${String(_jsonStringifyNumber_1._jsonStringifyNumber(input.tokens))},${undefined === input.oversized ? "" : `"oversized":${undefined !== input.oversized ? _so3(input.oversized) : undefined},`}${undefined === input.charged ? "" : `"charged":${undefined !== input.charged ? String(_jsonStringifyNumber_1._jsonStringifyNumber(input.charged)) : undefined}`}`)}}`;
63
38
  const _so3 = input => `{"memory":${String(_jsonStringifyNumber_1._jsonStringifyNumber(input.memory))}}`;
64
39
  const _so4 = input => `{"ok":${String(input.ok)},"code":${"\"" + input.code + "\""},"error":${_jsonStringifyString_1._jsonStringifyString(input.error)}}`;
65
40
  const _su0 = input => (() => {
@@ -74,13 +49,13 @@ const stringifyReply = (() => {
74
49
  else
75
50
  _throwTypeGuardError_1._throwTypeGuardError({
76
51
  method: "typia.json.createStringify",
77
- expected: "(CpuLeaseProgress | CpuLeaseQueued | { ok: true; lease: \"granted\"; tokens: number; oversized?: Oversized | undefined; } | { ok: false; code: \"invalid-request\"; error: string; })",
52
+ expected: "(CpuLeaseProgress | CpuLeaseQueued | { ok: true; lease: \"granted\"; tokens: number; oversized?: Oversized | undefined; charged?: number | undefined; } | { ok: false; code: \"invalid-request\"; error: string; })",
78
53
  value: input
79
54
  });
80
55
  })();
81
56
  const _io0 = input => _ip0(input) && "number" === typeof input.progress;
82
57
  const _io1 = input => _ip0(input) && "queued" === input.lease && ("number" === typeof input.server && _isTypeUint32_1._isTypeUint32(input.server));
83
- const _io2 = input => _ip0(input) && "granted" === input.lease && "number" === typeof input.tokens && (undefined === input.oversized || "object" === typeof input.oversized && null !== input.oversized && _io3(input.oversized));
58
+ const _io2 = input => _ip0(input) && "granted" === input.lease && "number" === typeof input.tokens && (undefined === input.oversized || "object" === typeof input.oversized && null !== input.oversized && _io3(input.oversized)) && (undefined === input.charged || "number" === typeof input.charged);
84
59
  const _io3 = input => "number" === typeof input.memory && _isTypeUint64_1._isTypeUint64(input.memory);
85
60
  const _io4 = input => false === input.ok && "invalid-request" === input.code && "string" === typeof input.error;
86
61
  return input => _su0(input);
@@ -101,19 +76,6 @@ const CLOSED_WHILE_MADE = 'the grant that lends this CPU pool closed while the p
101
76
  const run = promisify(execFile);
102
77
  /** One framed reply line. The newline is the framing; every reply goes through here. */
103
78
  const frame = (reply) => `${stringifyReply(reply)}\n`;
104
- /**
105
- * The inherited descriptor, or null when malformed or absent. A missing or bare count, or an empty
106
- * or NUL-bearing endpoint, proves no allocation; the count is a positive whole number. A valid
107
- * descriptor still needs a live endpoint grant.
108
- */
109
- export function readInheritedCpuPool(environment) {
110
- const socket = environment[CPU_POOL_ENV];
111
- const tokens = parseInheritedTokens(environment[CPU_TOKENS_ENV]);
112
- if (socket === undefined || socket.length === 0 || socket.includes('\0') || tokens === null) {
113
- return null;
114
- }
115
- return { socket, tokens };
116
- }
117
79
  const isOversized = (() => {
118
80
  const _io0 = (input, _exceptionable = true) => "number" === typeof input.memory && _isTypeUint64_1._isTypeUint64(input.memory) && (1 === Object.keys(input).length || Object.keys(input).every(key => {
119
81
  if (["memory"].some(prop => key === prop))
@@ -127,18 +89,24 @@ const isOversized = (() => {
127
89
  })();
128
90
  /**
129
91
  * Ask the immediate parent pool for `want` tokens, charged `memory` bytes: the task's last recorded peak, or null when
130
- * it has none. The inherited count does not bound the ask: a pool grants at most what it holds. A granted lease is the
131
- * child's local allocation: its connection is the grant, and closing it returns only that allocation. An absent
132
- * endpoint grants nothing and refuses nothing; a connected endpoint's refusal or protocol failure must fail admission
133
- * rather than run the child beside its parent's count.
92
+ * it has none. `context.parallelism` is the tokens the task was lent when that peak was measured; the pool charges the
93
+ * peak in proportion to the tokens it lends now when that is more, and lends fewer when the run's memory holds no more.
94
+ * The inherited count does not bound the ask: a pool grants at most what it holds. A granted lease is the child's local
95
+ * allocation: its connection is the grant, and closing it returns only that allocation. An absent endpoint grants
96
+ * nothing and refuses nothing; a connected endpoint's refusal or protocol failure must fail admission rather than run
97
+ * the child beside its parent's count.
134
98
  */
135
- export async function takeParentCpuTokens(parent, want, memory) {
99
+ export async function takeParentCpuTokens(parent, want, memory, context = {}) {
100
+ const { parallelism, task, alone } = context;
136
101
  if (!Number.isSafeInteger(want) || want < 1) {
137
102
  throw new RangeError(`a child asks its parent pool for a positive count, not ${want} (pool ${parent.tokens})`);
138
103
  }
139
104
  if (memory !== null && (!Number.isSafeInteger(memory) || memory < 0)) {
140
105
  throw new RangeError(`a child is charged a whole number of bytes, not ${memory}`);
141
106
  }
107
+ if (parallelism !== undefined && (!Number.isSafeInteger(parallelism) || parallelism < 1)) {
108
+ throw new RangeError(`a child's charge is measured under a positive count of tokens, not ${parallelism}`);
109
+ }
142
110
  if (parent.socket.length === 0 || parent.socket.includes('\0')) {
143
111
  return {
144
112
  granted: false,
@@ -146,12 +114,18 @@ export async function takeParentCpuTokens(parent, want, memory) {
146
114
  reason: `the parent CPU pool's endpoint ${JSON.stringify(parent.socket)} is not a socket path`,
147
115
  };
148
116
  }
149
- const request = memory === null ? { op: 'cpu-pool', want } : { op: 'cpu-pool', want, memory };
117
+ const request = {
118
+ op: 'cpu-pool',
119
+ want,
120
+ ...(memory === null ? {} : { memory, ...(parallelism === undefined ? {} : { parallelism }) }),
121
+ ...(task === undefined ? {} : { task }),
122
+ ...(alone === true ? { alone: true } : {}),
123
+ };
150
124
  const lease = await holdLease(parent.socket, request, 'the parent CPU pool', new CpuLeaseWait());
151
125
  if (!lease.granted) {
152
126
  return lease;
153
127
  }
154
- const { tokens, oversized } = lease.answer;
128
+ const { tokens, oversized, charged } = lease.answer;
155
129
  if (typeof tokens !== 'number' || !Number.isInteger(tokens) || tokens < 1 || tokens > want) {
156
130
  lease.release();
157
131
  return {
@@ -168,7 +142,22 @@ export async function takeParentCpuTokens(parent, want, memory) {
168
142
  reason: `the parent CPU pool named its memory ${JSON.stringify(oversized)} in a grant, which is no byte count`,
169
143
  };
170
144
  }
171
- return { granted: true, tokens, oversized: oversized ?? null, release: lease.release, closed: lease.closed };
145
+ if (charged !== undefined && !(typeof charged === 'number' && Number.isSafeInteger(charged) && charged >= 0)) {
146
+ lease.release();
147
+ return {
148
+ granted: false,
149
+ cause: 'other',
150
+ reason: `the parent CPU pool charged ${JSON.stringify(charged)} in a grant, which is no byte count`,
151
+ };
152
+ }
153
+ return {
154
+ granted: true,
155
+ tokens,
156
+ charged: charged ?? null,
157
+ oversized: oversized ?? null,
158
+ release: lease.release,
159
+ closed: lease.closed,
160
+ };
172
161
  }
173
162
  /** A thrown value's message. */
174
163
  const describe = (error) => (error instanceof Error ? error.message : String(error));
@@ -304,10 +293,11 @@ function watchFifo(path, took, failed) {
304
293
  /**
305
294
  * Make a pool of `tokens` that lives while `closed` does: when `closed` resolves the pool refuses
306
295
  * grants, ends its child connections, drains its jobserver fifo and removes its directory. A run's own
307
- * pool charges its grants against `memory`, the run's memory in bytes; a sub-pool charges none (null).
296
+ * pool charges its grants against `memory` and says each grant with what the run's bound holds live
297
+ * (`liveUsage`); a sub-pool charges none (null).
308
298
  * Setup failures are values; a non-positive or unsafe count is a programmer bug and throws.
309
299
  */
310
- export async function createCpuPool(tokens, closed, memory) {
300
+ export async function createCpuPool(tokens, closed, memory, liveUsage = readLiveUsage) {
311
301
  if (!Number.isSafeInteger(tokens) || tokens < 1) {
312
302
  throw new RangeError(`a CPU pool holds a positive safe integer of tokens, not ${tokens}`);
313
303
  }
@@ -337,6 +327,9 @@ export async function createCpuPool(tokens, closed, memory) {
337
327
  let slotLent = false;
338
328
  // The bytes the children holding grants are charged, against `memory`.
339
329
  let heldMemory = 0;
330
+ // The children holding a grant, and whether one of them runs alone.
331
+ let holders = 0;
332
+ let aloneHolder = false;
340
333
  // The readability wait while the queue's head finds no token, else undefined.
341
334
  let watch;
342
335
  let closing = false;
@@ -417,20 +410,32 @@ export async function createCpuPool(tokens, closed, memory) {
417
410
  return;
418
411
  }
419
412
  }
420
- const admission = admit(queue, inHand + (slotLent ? 0 : 1), memory === null ? null : { capacity: memory, held: heldMemory });
413
+ const admission = admit(queue, inHand + (slotLent ? 0 : 1), memory === null ? null : { capacity: memory.bytes, held: heldMemory }, { holders, alone: aloneHolder });
421
414
  queue.splice(0, admission.grants.length);
422
- for (const { request, tokens: granted, oversized } of admission.grants) {
415
+ for (const { request, tokens: granted, charge, oversized } of admission.grants) {
423
416
  // Fifo bytes first, the implicit slot last.
424
417
  const bytes = Math.min(granted, inHand);
425
418
  inHand -= bytes;
426
419
  const slot = granted > bytes;
427
420
  if (slot)
428
421
  slotLent = true;
429
- heldMemory += request.memory;
430
- request.child.phase = { kind: 'holding', bytes, slot, memory: request.memory };
431
- request.child.socket.write(frame(oversized && memory !== null
432
- ? { ok: true, lease: 'granted', tokens: granted, oversized: { memory } }
433
- : { ok: true, lease: 'granted', tokens: granted }));
422
+ heldMemory += charge;
423
+ holders += 1;
424
+ if (request.alone === true)
425
+ aloneHolder = true;
426
+ request.child.phase = { kind: 'holding', bytes, slot, memory: charge, alone: request.alone === true };
427
+ request.child.socket.write(frame(memory === null
428
+ ? { ok: true, lease: 'granted', tokens: granted }
429
+ : oversized
430
+ ? { ok: true, lease: 'granted', tokens: granted, charged: charge, oversized: { memory: memory.bytes } }
431
+ : { ok: true, lease: 'granted', tokens: granted, charged: charge }));
432
+ if (memory !== null) {
433
+ const live = liveUsage(memory.source);
434
+ const held = live.ok
435
+ ? `${live.usage.anon + live.usage.shmem} bytes (anon ${live.usage.anon}, shmem ${live.usage.shmem}, ${live.usage.source})`
436
+ : `unread (${live.reason})`;
437
+ process.stderr.write(`bounded-exec: the CPU pool at ${socketPath} granted ${request.task ?? 'a leaf'} ${granted} of ${request.want} tokens charged ${charge} bytes: holders charged ${heldMemory} of ${memory.bytes} bytes, live anon+shmem ${held}\n`);
438
+ }
434
439
  }
435
440
  if (admission.waiting === 'cpu') {
436
441
  watchForTokens();
@@ -467,6 +472,9 @@ export async function createCpuPool(tokens, closed, memory) {
467
472
  if (child.phase.slot)
468
473
  slotLent = false;
469
474
  heldMemory -= child.phase.memory;
475
+ holders -= 1;
476
+ if (child.phase.alone)
477
+ aloneHolder = false;
470
478
  if (!closing)
471
479
  returnTokens(child.phase.bytes);
472
480
  }
@@ -528,7 +536,22 @@ export async function createCpuPool(tokens, closed, memory) {
528
536
  }
529
537
  const request = parsed;
530
538
  child.phase = { kind: 'queued' };
531
- queue.push({ child, want: request.want, memory: request.memory ?? 0 });
539
+ const waiter = {
540
+ child,
541
+ want: request.want,
542
+ memory: request.memory ?? 0,
543
+ parallelism: request.parallelism,
544
+ task: request.task,
545
+ alone: request.alone,
546
+ };
547
+ if (request.alone === true) {
548
+ // A leaf the kernel killed has waited once already: it goes ahead of the leaves that have not run.
549
+ const at = queue.findIndex((waiting) => waiting.alone !== true);
550
+ queue.splice(at === -1 ? queue.length : at, 0, waiter);
551
+ }
552
+ else {
553
+ queue.push(waiter);
554
+ }
532
555
  socket.write(frame({ ok: true, lease: 'queued', server: process.pid }));
533
556
  drain(0);
534
557
  });
@@ -630,17 +653,10 @@ export async function createCpuPool(tokens, closed, memory) {
630
653
  if (closing) {
631
654
  return abort(CLOSED_WHILE_MADE);
632
655
  }
633
- // Cargo and make read the fifo from here; `-j` with a jobserver names no count of its own.
634
- const jobserverFlags = `-j --jobserver-auth=fifo:${fifoPath}`;
635
656
  return {
636
657
  ok: true,
637
658
  pool: {
638
- environment: {
639
- [CPU_POOL_ENV]: socketPath,
640
- [CPU_TOKENS_ENV]: String(tokens),
641
- CARGO_MAKEFLAGS: jobserverFlags,
642
- MAKEFLAGS: jobserverFlags,
643
- },
659
+ environment: poolEnvironment(socketPath, tokens, fifoPath),
644
660
  close: shutdown,
645
661
  },
646
662
  };
@@ -1,7 +1,8 @@
1
- import { type InheritedCpuPool } from './cpu-pool.js';
1
+ import { type QueueHost } from './admission-queue.js';
2
2
  import { captureHangDiagnostics } from './hang-diagnostics.js';
3
- import { snapshotMbxQueue } from './mbx-queue.js';
3
+ import { type RunOutcome } from './memory-retry.js';
4
4
  import { type SubtreeOwner } from './owned-subtree.js';
5
+ import { type InheritedCpuPool } from './pool-environment.js';
5
6
  import { RamTempVolume } from './ram-temp.js';
6
7
  import type { BoundedExecOptions } from './schema.js';
7
8
  import { type BoundedExecTask } from './verdict.js';
@@ -27,10 +28,7 @@ export interface NxTaskContext {
27
28
  }>;
28
29
  };
29
30
  }
30
- export interface BoundedExecResult {
31
- success: boolean;
32
- terminalOutput: string;
33
- }
31
+ export type BoundedExecResult = RunOutcome;
34
32
  export interface ProcessTreeKiller {
35
33
  kill(pid: number, signal: NodeJS.Signals): Promise<void>;
36
34
  }
@@ -70,6 +68,6 @@ export declare function recordContext(context: NxTaskContext): BoundedExecContex
70
68
  * connection closes while the command runs (the lending parent's exit) ends its accounting, never the command: the
71
69
  * command runs on over budget to its own exit, and the run says so.
72
70
  */
73
- export declare function runBoundedExec(options: BoundedExecOptions, context: BoundedExecContext, killer: ProcessTreeKiller, tempVolume: TempVolume | null, inherited?: InheritedCpuPool | null, captureDiagnostics?: typeof captureHangDiagnostics, owner?: SubtreeOwner | null, queueHost?: Parameters<typeof snapshotMbxQueue>[3]): Promise<BoundedExecResult>;
71
+ export declare function runBoundedExec(options: BoundedExecOptions, context: BoundedExecContext, killer: ProcessTreeKiller, tempVolume: TempVolume | null, inherited?: InheritedCpuPool | null, captureDiagnostics?: typeof captureHangDiagnostics, owner?: SubtreeOwner | null, queueHost?: QueueHost): Promise<BoundedExecResult>;
74
72
  export declare function createProcessTreeKiller(): ProcessTreeKiller;
75
73
  //# sourceMappingURL=executor.d.ts.map