Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(frontend): refuse the tsgo check when sandbox headroom is too low (#121928)

Alessandro Pogliaghib531fd2760a2
diff --git a/frontend/bin/typescript-check.mjs b/frontend/bin/typescript-check.mjs
index f7cd68048c1f..6a7cb64dc2ae 100644
--- a/frontend/bin/typescript-check.mjs
+++ b/frontend/bin/typescript-check.mjs
@@ -5,10 +5,9 @@ import os from 'os'
import path from 'path'
import { fileURLToPath } from 'url'
-const GIB = 1024 ** 3
+import { tsgoPlan } from './typescriptCheckPlan.mjs'
+
const WATCHDOG_RATIO = 0.85
-const UNTUNED_MIN_HEADROOM_GIB = 24
-const MIN_GOMEMLIMIT_GIB = 6
const frontendDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
function readNumber(file, key) {
@@ -17,8 +16,8 @@ function readNumber(file, key) {
const value = key
? text
.split('\n')
- .find((line) => line.startsWith(`${key} `))
- ?.split(' ')[1]
+ .map((line) => line.split(/\s+/))
+ .find(([name]) => name === key)?.[1]
: text
return value === undefined ? null : Number(value.trim())
} catch {
@@ -35,6 +34,15 @@ function cgroupHeadroomBytes(dir, limitFile, currentFile, inactiveKey) {
return WATCHDOG_RATIO * limit - current + (readNumber(`${dir}/memory.stat`, inactiveKey) ?? 0)
}
+function meminfoHeadroomBytes() {
+ const total = readNumber('/proc/meminfo', 'MemTotal:')
+ const available = readNumber('/proc/meminfo', 'MemAvailable:')
+ if (!total || available === null || Number.isNaN(available)) {
+ return null
+ }
+ return (WATCHDOG_RATIO * total - (total - available)) * 1024
+}
+
function headroomBytes() {
if (process.platform !== 'linux') {
return null
@@ -47,19 +55,23 @@ function headroomBytes() {
'memory.usage_in_bytes',
'total_inactive_file'
) ??
+ meminfoHeadroomBytes() ??
os.freemem() - (1 - WATCHDOG_RATIO) * os.totalmem()
)
}
-const headroom = headroomBytes()
-const constrained = headroom !== null && headroom < UNTUNED_MIN_HEADROOM_GIB * GIB
+const plan = tsgoPlan(headroomBytes())
+if (plan.mode === 'refuse') {
+ console.error(plan.message)
+ process.exit(75)
+}
+const constrained = plan.mode === 'constrained'
const args = ['--noEmit', ...(constrained ? ['--checkers', '1'] : []), ...process.argv.slice(2)]
let env = process.env
if (constrained) {
- const memoryLimit = `${Math.max(MIN_GOMEMLIMIT_GIB, Math.floor(headroom / GIB / 2))}GiB`
- env = { GOMEMLIMIT: memoryLimit, ...process.env }
+ env = { GOMEMLIMIT: plan.goMemLimit, ...process.env }
console.info(
- `${(headroom / GIB).toFixed(1)} GiB of memory headroom: running tsgo with one checker and GOMEMLIMIT=${env.GOMEMLIMIT}.`
+ `${plan.headroomGib} GiB of memory headroom: running tsgo with one checker and GOMEMLIMIT=${env.GOMEMLIMIT}.`
)
}
diff --git a/frontend/bin/typescriptCheckPlan.mjs b/frontend/bin/typescriptCheckPlan.mjs
new file mode 100644
index 000000000000..b069497bb881
--- /dev/null
+++ b/frontend/bin/typescriptCheckPlan.mjs
@@ -0,0 +1,19 @@
+const GIB = 1024 ** 3
+const UNTUNED_MIN_HEADROOM_GIB = 24
+const MIN_GOMEMLIMIT_GIB = 6
+const HEADROOM_AT_MIN_GOMEMLIMIT_GIB = 9
+
+export function tsgoPlan(headroom) {
+ if (headroom === null || headroom >= UNTUNED_MIN_HEADROOM_GIB * GIB) {
+ return { mode: 'full' }
+ }
+ const headroomGib = (Math.max(0, headroom) / GIB).toFixed(1)
+ if (headroom < HEADROOM_AT_MIN_GOMEMLIMIT_GIB * GIB) {
+ return {
+ mode: 'refuse',
+ message: `Only ${headroomGib} GiB of memory headroom is free, and the TypeScript check needs ${HEADROOM_AT_MIN_GOMEMLIMIT_GIB} GiB. Stop background processes such as a Storybook or app dev server, then run this check again.`,
+ }
+ }
+ const goMemLimit = `${Math.max(MIN_GOMEMLIMIT_GIB, Math.floor(headroom / GIB / 2))}GiB`
+ return { mode: 'constrained', goMemLimit, headroomGib }
+}
diff --git a/frontend/bin/typescriptCheckPlan.test.ts b/frontend/bin/typescriptCheckPlan.test.ts
new file mode 100644
index 000000000000..a074aae7e31c
--- /dev/null
+++ b/frontend/bin/typescriptCheckPlan.test.ts
@@ -0,0 +1,16 @@
+import { tsgoPlan } from './typescriptCheckPlan.mjs'
+
+const GIB = 1024 ** 3
+
+describe('tsgoPlan', () => {
+ it.each([
+ ['unknown headroom', null, { mode: 'full' }],
+ ['24 GiB of headroom', 24 * GIB, { mode: 'full' }],
+ ['20 GiB of headroom', 20 * GIB, { mode: 'constrained', goMemLimit: '10GiB', headroomGib: '20.0' }],
+ ['9 GiB of headroom', 9 * GIB, { mode: 'constrained', goMemLimit: '6GiB', headroomGib: '9.0' }],
+ ['8 GiB of headroom', 8 * GIB, { mode: 'refuse', message: expect.stringContaining('Only 8.0 GiB') }],
+ ['negative headroom', -1 * GIB, { mode: 'refuse', message: expect.stringContaining('Only 0.0 GiB') }],
+ ])('with %s', (_, headroom, expected) => {
+ expect(tsgoPlan(headroom)).toEqual(expected)
+ })
+})
diff --git a/frontend/src/AGENTS.md b/frontend/src/AGENTS.md
index 0060c8becdea..2cfa894b2f91 100644
--- a/frontend/src/AGENTS.md
+++ b/frontend/src/AGENTS.md
@@ -120,7 +120,7 @@ A whole subtree of machine data (an event feed, an ID column) opts out at its co
These are slow; run them at the right moment, not after every edit.
-- **TypeScript check** — `pnpm --filter=@posthog/frontend typescript:check` (runs `tsgo --noEmit` over the whole app; with under 24 GiB of memory headroom below the sandbox watchdog threshold it runs one checker under a Go heap limit, about three times slower). Rely on the editor/LSP while editing; run the full check **once before you call the work done**, not per-edit.
+- **TypeScript check** — `pnpm --filter=@posthog/frontend typescript:check` (runs `tsgo --noEmit` over the whole app; with under 24 GiB of memory headroom below the sandbox watchdog threshold it runs one checker under a Go heap limit, about three times slower, and under 9 GiB it refuses to start and tells you which background processes to stop). Rely on the editor/LSP while editing; run the full check **once before you call the work done**, not per-edit.
- **kea typegen** (inline types) — regenerates automatically via `typegen:watch` while the dev server runs (`./bin/start` / `hogli start`). If you must regen one logic without the server, use `pnpm --filter=@posthog/frontend typegen:file <path>`. **Don't routinely run the full `typegen:write`** — it's a heavy whole-repo pass (multi-GB heap).
All logic files use **inline types** by default: a generated `MakeLogicType` block sits above each `kea()` call, marked "Generated by kea-typegen". Agents may update those blocks (typegen reconciles them anyway); humans should let the typegen commands keep them updated. Connected values/actions carry a `// sourceLogic` comment and sort first, grouped by source logic.
- **`hogli build:openapi`** — only needed when you changed a **backend serializer/viewset**. A pure `.tsx`/`.ts` change never needs it. Don't run it speculatively.