feat(tui): add heap snapshot functionality for TUI and server (#19028)

pull/18538/head^2
Dax 2026-03-24 18:20:11 -04:00 committed by GitHub
parent 1398674e53
commit 15dc33d1a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 8 deletions

View File

@ -1,5 +1,17 @@
create UPCOMING_CHANGELOG.md
it should have sections
```
# TUI
# Desktop
# Core
# Misc
```
go through each PR merged since the last tag go through each PR merged since the last tag
for each PR spawn a subagent to summarize what the PR was about. focus on user facing changes. if it was entirely internal or code related you can ignore it. also skip docs updates. each subagent should append its summary to UPCOMING_CHANGELOG.md for each PR spawn a subagent to summarize what the PR was about. focus on user facing changes. if it was entirely internal or code related you can ignore it. also skip docs updates. each subagent should append its summary to UPCOMING_CHANGELOG.md into the appropriate section.
once that is done, read UPCOMING_CHANGELOG.md and group it into sections for better readability. make sure all PR references are preserved

View File

@ -110,6 +110,7 @@ export function tui(input: {
url: string url: string
args: Args args: Args
config: TuiConfig.Info config: TuiConfig.Info
onSnapshot?: () => Promise<string[]>
directory?: string directory?: string
fetch?: typeof fetch fetch?: typeof fetch
headers?: RequestInit["headers"] headers?: RequestInit["headers"]
@ -160,7 +161,7 @@ export function tui(input: {
<FrecencyProvider> <FrecencyProvider>
<PromptHistoryProvider> <PromptHistoryProvider>
<PromptRefProvider> <PromptRefProvider>
<App /> <App onSnapshot={input.onSnapshot} />
</PromptRefProvider> </PromptRefProvider>
</PromptHistoryProvider> </PromptHistoryProvider>
</FrecencyProvider> </FrecencyProvider>
@ -201,7 +202,7 @@ export function tui(input: {
}) })
} }
function App() { function App(props: { onSnapshot?: () => Promise<string[]> }) {
const route = useRoute() const route = useRoute()
const dimensions = useTerminalDimensions() const dimensions = useTerminalDimensions()
const renderer = useRenderer() const renderer = useRenderer()
@ -627,11 +628,11 @@ function App() {
title: "Write heap snapshot", title: "Write heap snapshot",
category: "System", category: "System",
value: "app.heap_snapshot", value: "app.heap_snapshot",
onSelect: (dialog) => { onSelect: async (dialog) => {
const path = writeHeapSnapshot() const files = await props.onSnapshot?.()
toast.show({ toast.show({
variant: "info", variant: "info",
message: `Heap snapshot written to ${path}`, message: `Heap snapshot written to ${files?.join(", ")}`,
duration: 5000, duration: 5000,
}) })
dialog.clear() dialog.clear()

View File

@ -14,6 +14,7 @@ import type { EventSource } from "./context/sdk"
import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32" import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
import { TuiConfig } from "@/config/tui" import { TuiConfig } from "@/config/tui"
import { Instance } from "@/project/instance" import { Instance } from "@/project/instance"
import { writeHeapSnapshot } from "v8"
declare global { declare global {
const OPENCODE_WORKER_PATH: string const OPENCODE_WORKER_PATH: string
@ -201,6 +202,11 @@ export const TuiThreadCommand = cmd({
try { try {
await tui({ await tui({
url: transport.url, url: transport.url,
async onSnapshot() {
const tui = writeHeapSnapshot("tui.heapsnapshot")
const server = await client.call("snapshot", undefined)
return [tui, server]
},
config, config,
directory: cwd, directory: cwd,
fetch: transport.fetch, fetch: transport.fetch,

View File

@ -10,6 +10,7 @@ import { GlobalBus } from "@/bus/global"
import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2" import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2"
import { Flag } from "@/flag/flag" import { Flag } from "@/flag/flag"
import { setTimeout as sleep } from "node:timers/promises" import { setTimeout as sleep } from "node:timers/promises"
import { writeHeapSnapshot } from "node:v8"
await Log.init({ await Log.init({
print: process.argv.includes("--print-logs"), print: process.argv.includes("--print-logs"),
@ -117,6 +118,10 @@ export const rpc = {
body, body,
} }
}, },
snapshot() {
const result = writeHeapSnapshot("server.heapsnapshot")
return result
},
async server(input: { port: number; hostname: string; mdns?: boolean; cors?: string[] }) { async server(input: { port: number; hostname: string; mdns?: boolean; cors?: string[] }) {
if (server) await server.stop(true) if (server) await server.stop(true)
server = await Server.listen(input) server = await Server.listen(input)