Compare commits

...

1 Commits

Author SHA1 Message Date
Kit Langton ee973d3870 feat: unwrap Heap namespace to flat exports + barrel 2026-04-16 07:29:46 -04:00
4 changed files with 42 additions and 43 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ import { Config } from "@/config"
import { GlobalBus } from "@/bus/global" import { GlobalBus } from "@/bus/global"
import { Flag } from "@/flag/flag" import { Flag } from "@/flag/flag"
import { writeHeapSnapshot } from "node:v8" import { writeHeapSnapshot } from "node:v8"
import { Heap } from "@/cli/heap" import { Heap } from "@/cli"
import { AppRuntime } from "@/effect/app-runtime" import { AppRuntime } from "@/effect/app-runtime"
await Log.init({ await Log.init({
+39 -41
View File
@@ -8,52 +8,50 @@ const log = Log.create({ service: "heap" })
const MINUTE = 60_000 const MINUTE = 60_000
const LIMIT = 2 * 1024 * 1024 * 1024 const LIMIT = 2 * 1024 * 1024 * 1024
export namespace Heap { let timer: Timer | undefined
let timer: Timer | undefined let lock = false
let lock = false let armed = true
let armed = true
export function start() { export function start() {
if (!Flag.OPENCODE_AUTO_HEAP_SNAPSHOT) return if (!Flag.OPENCODE_AUTO_HEAP_SNAPSHOT) return
if (timer) return if (timer) return
const run = async () => { const run = async () => {
if (lock) return if (lock) return
const stat = process.memoryUsage() const stat = process.memoryUsage()
if (stat.rss <= LIMIT) { if (stat.rss <= LIMIT) {
armed = true armed = true
return return
} }
if (!armed) return if (!armed) return
lock = true lock = true
armed = false armed = false
const file = path.join( const file = path.join(
Global.Path.log, Global.Path.log,
`heap-${process.pid}-${new Date().toISOString().replace(/[:.]/g, "")}.heapsnapshot`, `heap-${process.pid}-${new Date().toISOString().replace(/[:.]/g, "")}.heapsnapshot`,
) )
log.warn("heap usage exceeded limit", { log.warn("heap usage exceeded limit", {
rss: stat.rss, rss: stat.rss,
heap: stat.heapUsed, heap: stat.heapUsed,
file, file,
})
await Promise.resolve()
.then(() => writeHeapSnapshot(file))
.catch((err) => {
log.error("failed to write heap snapshot", {
error: err instanceof Error ? err.message : String(err),
file,
})
}) })
await Promise.resolve() lock = false
.then(() => writeHeapSnapshot(file))
.catch((err) => {
log.error("failed to write heap snapshot", {
error: err instanceof Error ? err.message : String(err),
file,
})
})
lock = false
}
timer = setInterval(() => {
void run()
}, MINUTE)
timer.unref?.()
} }
timer = setInterval(() => {
void run()
}, MINUTE)
timer.unref?.()
} }
+1
View File
@@ -0,0 +1 @@
export * as Heap from "./heap"
+1 -1
View File
@@ -36,7 +36,7 @@ import { JsonMigration } from "./storage"
import { Database } from "./storage" import { Database } from "./storage"
import { errorMessage } from "./util/error" import { errorMessage } from "./util/error"
import { PluginCommand } from "./cli/cmd/plug" import { PluginCommand } from "./cli/cmd/plug"
import { Heap } from "./cli/heap" import { Heap } from "./cli"
import { drizzle } from "drizzle-orm/bun-sqlite" import { drizzle } from "drizzle-orm/bun-sqlite"
process.on("unhandledRejection", (e) => { process.on("unhandledRejection", (e) => {