@hydranium/core 1.0.0-next.75 → 1.0.0-next.77

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.
@@ -0,0 +1,108 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 CrossBreeze, EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the MIT License which is available in the project root.
6
+ *
7
+ * SPDX-License-Identifier: MIT
8
+ ********************************************************************************/
9
+
10
+ /*
11
+ * Choosing `--max-old-space-size` for a child process, which is a decision no
12
+ * caller gets right by picking a number.
13
+ *
14
+ * A fixed ceiling is sized for the machine the author had. Pass 8192 to a
15
+ * process under a 2 GiB cgroup limit and V8 lets old space grow past the limit
16
+ * without ever collecting hard, so the kernel OOM-kills the whole container
17
+ * before V8 has any reason to act — the parent sees a signal, not a heap error,
18
+ * and everything sharing the cgroup dies with it. Pass nothing on a large
19
+ * desktop and V8 sizes from physical RAM, which for a memory-heavy tool is well
20
+ * under what it needs.
21
+ *
22
+ * Both are the same mistake: a ceiling stated without reference to the limit
23
+ * that is actually in force. The rule here is to name the desktop default
24
+ * explicitly and step aside inside a container, where Node derives its own
25
+ * ceiling from the cgroup limit and a runaway heap therefore fails INSIDE the
26
+ * child — attributable, and reported as a heap error against one process rather
27
+ * than as a kill against every process in the cgroup.
28
+ */
29
+
30
+ import * as os from 'node:os';
31
+
32
+ /** Options for {@link heapCeilingArgs}. */
33
+ export interface HeapCeilingOptions {
34
+ /**
35
+ * Ceiling in MiB to pass when no cgroup limit is in force. Name the value a
36
+ * memory-heavy run on a workstation needs; it is ignored inside a container.
37
+ */
38
+ readonly desktopDefaultMb: number;
39
+ /**
40
+ * Caller-supplied override, typically an environment variable, and typically
41
+ * `undefined`. A finite value at or above {@link minMb} wins everywhere,
42
+ * container or not — an operator who states a number has decided. An explicit
43
+ * `0` means "let Node decide" and emits no flag. Anything unparseable is
44
+ * REPORTED through {@link warn} rather than ignored: `8G` is a natural thing
45
+ * to type into a variable measured in MiB, and silently dropping it leaves
46
+ * the operator believing a ceiling is in force.
47
+ */
48
+ readonly envValue?: string;
49
+ /**
50
+ * Smallest override to pass on. Below it, {@link warn} is called and NO flag
51
+ * is emitted: a value that small can only be a misconfiguration — GiB typed
52
+ * where MiB was meant — and V8 fatals at startup once it is small enough.
53
+ * Deliberately not clamped UP: running with a different number than the one
54
+ * configured hides the mistake instead of surfacing it. Omitted means no floor.
55
+ */
56
+ readonly minMb?: number;
57
+ /** Where a rejected override is reported. Defaults to `console.warn`. */
58
+ readonly warn?: (message: string) => void;
59
+ /** Cgroup limit in bytes; defaults to this process's. Injectable for tests. */
60
+ readonly constrained?: number;
61
+ /** Host physical memory in bytes; defaults to `os.totalmem()`. Injectable for tests. */
62
+ readonly total?: number;
63
+ }
64
+
65
+ /**
66
+ * Whether a cgroup memory limit is in force — i.e. whether the OOM killer is
67
+ * watching a ceiling lower than (or equal to) the machine's own.
68
+ *
69
+ * **`constrained > 0` is not the test, and that is the whole reason this is a
70
+ * named function.** With no limit set, cgroup v2 reports 2^64 and v1 reports
71
+ * ~2^63 rather than 0, so the naive test reads every desktop as a container.
72
+ * Neither sentinel can equal `os.totalmem()`, so comparing against the host
73
+ * total costs nothing and rules both out. The comparison is `<=` rather than
74
+ * `<` so a limit set to exactly the host's RAM still counts as a limit.
75
+ */
76
+ export function isMemoryConstrained(constrained = process.constrainedMemory?.() ?? 0, total = os.totalmem()): boolean {
77
+ return constrained > 0 && constrained <= total;
78
+ }
79
+
80
+ /**
81
+ * The `execArgv` entries that set a child's heap ceiling: `[]` to let Node size
82
+ * the heap itself, or a single `--max-old-space-size=<mb>`.
83
+ *
84
+ * Returns an array rather than a string so a caller can spread it
85
+ * unconditionally into an argv it is building.
86
+ */
87
+ export function heapCeilingArgs(options: HeapCeilingOptions): string[] {
88
+ const { desktopDefaultMb, envValue, minMb, warn = (message: string) => console.warn(message) } = options;
89
+ if (envValue !== undefined) {
90
+ const mb = Number(envValue);
91
+ // An explicit 0 is a decision — "let Node decide" — so it passes quietly.
92
+ // Anything else that is not a positive number is a typo, and the operator
93
+ // who typed it is the one person who cannot tell it was dropped.
94
+ if (!Number.isFinite(mb) || mb < 0) {
95
+ warn(`Ignoring the heap ceiling '${envValue}': expected a whole number of MiB. Node will size the heap instead.`);
96
+ return [];
97
+ }
98
+ if (mb === 0) {
99
+ return [];
100
+ }
101
+ if (minMb !== undefined && mb < minMb) {
102
+ warn(`Ignoring a heap ceiling of ${envValue} MiB: below the ${minMb} MiB minimum. Node will size the heap instead.`);
103
+ return [];
104
+ }
105
+ return [`--max-old-space-size=${Math.floor(mb)}`];
106
+ }
107
+ return isMemoryConstrained(options.constrained, options.total) ? [] : [`--max-old-space-size=${desktopDefaultMb}`];
108
+ }
package/src/node/index.ts CHANGED
@@ -35,6 +35,7 @@ export * from './server-diagnostics.js';
35
35
  // entry alongside the default policy.
36
36
  export * from './ast-ground-truth.js';
37
37
  export * from './event-loop-monitor.js';
38
+ export * from './heap-ceiling.js';
38
39
  export * from './latency-from-env.js';
39
40
  export * from './measure-memory.js';
40
41
  export * from './memory-monitor.js';