Skip to content

Commit e89f5f7

Browse files
committed
feat(opencode): add automatic heap snapshots for high-memory cli processes (anomalyco#20788)
Cherry-picked from upstream sst/opencode aa2239d (Dax). Drops the TUI worker.ts hunk (file structure differs in this fork); keeps the self-contained Heap module + flag + bootstrap call. When OPENCODE_AUTO_HEAP_SNAPSHOT=1 is set, every minute the bootstrap samples process.memoryUsage().rss; once it exceeds 2GB the module writes a single heap snapshot into Global.Path.log and re-arms only after RSS drops below the limit. unref() ensures it never holds the event loop open. Useful for diagnosing sidecar memory regressions in the wild without requiring a running TUI debug session.
1 parent 5e44557 commit e89f5f7

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

packages/opencode/src/cli/heap.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import path from "path"
2+
import { writeHeapSnapshot } from "node:v8"
3+
import { Flag } from "@/flag/flag"
4+
import { Global } from "@/global"
5+
import { Log } from "@/util/log"
6+
7+
const log = Log.create({ service: "heap" })
8+
const MINUTE = 60_000
9+
const LIMIT = 2 * 1024 * 1024 * 1024
10+
11+
export namespace Heap {
12+
let timer: Timer | undefined
13+
let lock = false
14+
let armed = true
15+
16+
export function start() {
17+
if (!Flag.OPENCODE_AUTO_HEAP_SNAPSHOT) return
18+
if (timer) return
19+
20+
const run = async () => {
21+
if (lock) return
22+
23+
const stat = process.memoryUsage()
24+
if (stat.rss <= LIMIT) {
25+
armed = true
26+
return
27+
}
28+
if (!armed) return
29+
30+
lock = true
31+
armed = false
32+
const file = path.join(
33+
Global.Path.log,
34+
`heap-${process.pid}-${new Date().toISOString().replace(/[:.]/g, "")}.heapsnapshot`,
35+
)
36+
log.warn("heap usage exceeded limit", {
37+
rss: stat.rss,
38+
heap: stat.heapUsed,
39+
file,
40+
})
41+
42+
await Promise.resolve()
43+
.then(() => writeHeapSnapshot(file))
44+
.catch((err) => {
45+
log.error("failed to write heap snapshot", {
46+
error: err instanceof Error ? err.message : String(err),
47+
file,
48+
})
49+
})
50+
51+
lock = false
52+
}
53+
54+
timer = setInterval(() => {
55+
void run()
56+
}, MINUTE)
57+
timer.unref?.()
58+
}
59+
}

packages/opencode/src/flag/flag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function falsy(key: string) {
1212

1313
export namespace Flag {
1414
export const OPENCODE_AUTO_SHARE = truthy("OPENCODE_AUTO_SHARE")
15+
export const OPENCODE_AUTO_HEAP_SNAPSHOT = truthy("OPENCODE_AUTO_HEAP_SNAPSHOT")
1516
export const OPENCODE_GIT_BASH_PATH = process.env["OPENCODE_GIT_BASH_PATH"]
1617
export const OPENCODE_CONFIG = process.env["OPENCODE_CONFIG"]
1718
export declare const OPENCODE_TUI_CONFIG: string | undefined

packages/opencode/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import path from "path"
3737
import { Global } from "./global"
3838
import { JsonMigration } from "./storage/json-migration"
3939
import { Database } from "./storage/db"
40+
import { Heap } from "./cli/heap"
4041

4142
process.on("unhandledRejection", (e) => {
4243
Log.Default.error("rejection", {
@@ -83,6 +84,8 @@ let cli = yargs(hideBin(process.argv))
8384
process.env.OPENCODE = "1"
8485
process.env.OPENCODE_PID = String(process.pid)
8586

87+
Heap.start()
88+
8689
Log.create({ service: "startup" }).info("cli.bootstrap", {
8790
duration: Date.now() - start,
8891
print_logs: process.argv.includes("--print-logs"),

0 commit comments

Comments
 (0)