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): size the tsgo check to the memory the sandbox has (#121349)

Alessandro Pogliaghiba5f89ad7177
diff --git a/frontend/bin/typescript-check.mjs b/frontend/bin/typescript-check.mjs
new file mode 100644
index 000000000000..f7cd68048c1f
--- /dev/null
+++ b/frontend/bin/typescript-check.mjs
@@ -0,0 +1,74 @@
+#!/usr/bin/env node
+import { spawnSync } from 'child_process'
+import { readFileSync } from 'fs'
+import os from 'os'
+import path from 'path'
+import { fileURLToPath } from 'url'
+
+const GIB = 1024 ** 3
+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) {
+ try {
+ const text = readFileSync(file, 'utf8')
+ const value = key
+ ? text
+ .split('\n')
+ .find((line) => line.startsWith(`${key} `))
+ ?.split(' ')[1]
+ : text
+ return value === undefined ? null : Number(value.trim())
+ } catch {
+ return null
+ }
+}
+
+function cgroupHeadroomBytes(dir, limitFile, currentFile, inactiveKey) {
+ const limit = readNumber(`${dir}/${limitFile}`)
+ const current = readNumber(`${dir}/${currentFile}`)
+ if (!limit || !current || limit > 2 ** 50) {
+ return null
+ }
+ return WATCHDOG_RATIO * limit - current + (readNumber(`${dir}/memory.stat`, inactiveKey) ?? 0)
+}
+
+function headroomBytes() {
+ if (process.platform !== 'linux') {
+ return null
+ }
+ return (
+ cgroupHeadroomBytes('/sys/fs/cgroup', 'memory.max', 'memory.current', 'inactive_file') ??
+ cgroupHeadroomBytes(
+ '/sys/fs/cgroup/memory',
+ 'memory.limit_in_bytes',
+ 'memory.usage_in_bytes',
+ 'total_inactive_file'
+ ) ??
+ os.freemem() - (1 - WATCHDOG_RATIO) * os.totalmem()
+ )
+}
+
+const headroom = headroomBytes()
+const constrained = headroom !== null && headroom < UNTUNED_MIN_HEADROOM_GIB * GIB
+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 }
+ console.info(
+ `${(headroom / GIB).toFixed(1)} GiB of memory headroom: running tsgo with one checker and GOMEMLIMIT=${env.GOMEMLIMIT}.`
+ )
+}
+
+const result = spawnSync('tsgo', args, { cwd: frontendDir, env, stdio: 'inherit' })
+if (result.error) {
+ console.error(`Failed to run tsgo: ${result.error.message}`)
+ process.exit(1)
+}
+if (result.status === 0) {
+ console.info('No errors reported by tsgo.')
+}
+process.exit(result.status ?? 1)
diff --git a/frontend/package.json b/frontend/package.json
index 0b63cd010775..218b6ab744a5 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -38,7 +38,7 @@
"schema:build:json": "ts-node ./bin/build-schema-json.mjs && oxfmt ./src/queries/schema.json && ts-node ./bin/build-validators.mjs && oxfmt ./src/queries/validators.js",
"test": "SHARD_IDX=${SHARD_INDEX:-1}; SHARD_TOTAL=${SHARD_COUNT:-1}; echo $SHARD_IDX/$SHARD_TOTAL; pnpm build:products && jest --testPathPattern='(frontend/|products/|common/|packages/quill/|packages/llm-normalizer/)' --forceExit --shard=$SHARD_IDX/$SHARD_TOTAL",
"jest": "pnpm build:products && jest",
- "typescript:check": "tsgo --noEmit && echo \"No errors reported by tsgo.\"",
+ "typescript:check": "node ./bin/typescript-check.mjs",
"typescript:version": "tsgo --version",
"lint": "cd .. && oxlint --quiet",
"lint:fix": "cd .. && oxlint --fix --quiet",
diff --git a/frontend/src/AGENTS.md b/frontend/src/AGENTS.md
index 61d776d238f2..0060c8becdea 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). 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). 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.
diff --git a/packages/agent/packages/agent/src/server/memory-watchdog.test.ts b/packages/agent/packages/agent/src/server/memory-watchdog.test.ts
index 86b017a90e11..4c8ed0b8bef8 100644
--- a/packages/agent/packages/agent/src/server/memory-watchdog.test.ts
+++ b/packages/agent/packages/agent/src/server/memory-watchdog.test.ts
@@ -96,6 +96,11 @@ describe("memory watchdog", () => {
record: { ...KILL_RECORD, signal: 9, cmdline: ["node", "big.js"] },
expected: { ...KILL_PARAMS, signal: "SIGKILL" },
},
+ {
+ name: "a kill record with the heaviest member",
+ record: { ...KILL_RECORD, comm: "bash", top_comm: "tsgo" },
+ expected: { ...KILL_PARAMS, comm: "tsgo" },
+ },
{
name: "a pressure record",
record: { ...KILL_RECORD, event: "pressure" },
diff --git a/packages/agent/packages/agent/src/server/memory-watchdog.ts b/packages/agent/packages/agent/src/server/memory-watchdog.ts
index 4399e541fee3..ff3e802dc814 100644
--- a/packages/agent/packages/agent/src/server/memory-watchdog.ts
+++ b/packages/agent/packages/agent/src/server/memory-watchdog.ts
@@ -98,7 +98,10 @@ export function toProcessKilledParams(
}
return {
pid,
- comm: record.comm,
+ comm:
+ typeof record.top_comm === "string" && record.top_comm
+ ? record.top_comm
+ : record.comm,
treeRssBytes,
memoryCurrentBytes,
memoryLimitBytes,