Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5410533
fix(cli): cap file content at 256 KB in Snapshot.diffFull()
alex-alecu Mar 25, 2026
7f57983
fix(cli): strip before/after from TUI session_diff store
alex-alecu Mar 25, 2026
59d1bbb
perf(cli): use git cat-file -s to pre-check size before reading file …
alex-alecu Mar 25, 2026
2451764
fix(cli): scrub oversized diffs from stored session_diff on read
alex-alecu Mar 25, 2026
d7f73a1
fix(cli): use byte length for size check
alex-alecu Mar 25, 2026
1a07ad1
fix(cli): evict per-session data from TUI store on navigation
alex-alecu Mar 25, 2026
a3c8340
fix(cli): keep permission and question on evict
alex-alecu Mar 25, 2026
6878ddb
fix(cli): strip summary.diffs from messages in TUI store
alex-alecu Mar 25, 2026
1c4918c
Merge branch 'main' into fix/session-diff-memory-leak
alex-alecu Mar 25, 2026
6819ee7
fix(cli): restart worker on /new to reclaim native memory
alex-alecu Mar 26, 2026
10e3b12
fix(cli): evict child sessions recursively
alex-alecu Mar 26, 2026
1a0536d
fix(cli): detach RPC listener on cleanup
alex-alecu Mar 26, 2026
a7d3731
fix(cli): replay workspace after restart
alex-alecu Mar 26, 2026
5e38d95
chore: update source-links.md
alex-alecu Mar 26, 2026
1596cae
fix(cli): use subprocess instead of Worker thread for actual memory r…
alex-alecu Mar 26, 2026
5d611d6
revert: remove /new worker/subprocess restart
alex-alecu Mar 26, 2026
8b676f8
Merge branch 'main' into fix/session-diff-memory-leak
alex-alecu Mar 26, 2026
3ed648d
refactor(cli): extract max diff size constant
alex-alecu Mar 26, 2026
bfbfbb9
chore(cli): add kilocode_change markers
alex-alecu Mar 26, 2026
721b3d0
chore(cli): add missing kilocode_change markers
alex-alecu Mar 26, 2026
664ceec
refactor(cli): reuse MAX_DIFF_SIZE constant
alex-alecu Mar 26, 2026
1cb4b5c
refactor(cli): clean up code quality issues
alex-alecu Mar 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/opencode/src/cli/cmd/tui/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function tui(input: {
fetch?: typeof fetch
headers?: RequestInit["headers"]
events?: EventSource
restart?: () => Promise<void> // kilocode_change — worker restart callback for /new
}) {
// promise to prevent immediate exit
return new Promise<void>(async (resolve) => {
Expand Down Expand Up @@ -152,6 +153,7 @@ export function tui(input: {
fetch={input.fetch}
headers={input.headers}
events={input.events}
restart={input.restart}
>
<SyncProvider>
<ThemeProvider mode={mode}>
Expand Down Expand Up @@ -268,6 +270,17 @@ function App() {
})
// kilocode_change end

// kilocode_change start — evict per-session data from store when navigating away
createEffect(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR added createEffect in app.tsx that calls evict(prev) when navigating away from a session, intended to clean up memory. But it evicts ALL child sessions' data including pending permissions.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a3c8340 doesn't solve the problem when navigating to or from subagent views while there is a pending permission request or question.

on(
() => (route.data.type === "session" ? route.data.sessionID : undefined),
(current, prev) => {
if (prev && prev !== current) sync.session.evict(prev)
},
),
)
// kilocode_change end

// Update terminal window title based on current route and session
createEffect(() => {
if (!terminalTitleEnabled() || Flag.KILO_DISABLE_TERMINAL_TITLE) return
Expand Down Expand Up @@ -417,6 +430,11 @@ function App() {
initialPrompt: currentPrompt,
})
dialog.clear()
// kilocode_change start — restart worker to reclaim native/JSC memory.
// Fire-and-forget; the sync layer will re-bootstrap when the new worker
// emits server.instance.disposed.
sdk.restart?.()
// kilocode_change end
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/cli/cmd/tui/context/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const { use: useSDK, provider: SDKProvider } = createSimpleContext({
fetch?: typeof fetch
headers?: RequestInit["headers"]
events?: EventSource
restart?: () => Promise<void> // kilocode_change — worker restart callback
}) => {
const abort = new AbortController()
let workspaceID: string | undefined
Expand Down Expand Up @@ -112,6 +113,7 @@ export const { use: useSDK, provider: SDKProvider } = createSimpleContext({
directory: props.directory,
event: emitter,
fetch: props.fetch ?? fetch,
restart: props.restart, // kilocode_change — expose worker restart
setWorkspace(next?: string) {
if (workspaceID === next) return
workspaceID = next
Expand Down
65 changes: 49 additions & 16 deletions packages/opencode/src/cli/cmd/tui/context/sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
[sessionID: string]: SessionStatus
}
session_diff: {
[sessionID: string]: Snapshot.FileDiff[]
[sessionID: string]: Omit<Snapshot.FileDiff, "before" | "after">[]
Comment thread
alex-alecu marked this conversation as resolved.
Resolved
}
todo: {
[sessionID: string]: Todo[]
Expand Down Expand Up @@ -107,12 +107,38 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({

const sdk = useSDK()

const fullSyncedSessions = new Set<string>()

async function syncWorkspaces() {
const result = await sdk.client.experimental.workspace.list().catch(() => undefined)
if (!result?.data) return
setStore("workspaceList", reconcile(result.data))
}

function evict(sessionID: string) {
Comment thread
alex-alecu marked this conversation as resolved.
Resolved
setStore(
produce((draft) => {
const messages = draft.message[sessionID]
if (messages) {
for (const msg of messages) delete draft.part[msg.id]
}
delete draft.message[sessionID]
delete draft.session_diff[sessionID]
delete draft.session_status[sessionID]
delete draft.todo[sessionID]
}),
)
fullSyncedSessions.delete(sessionID)
}

// Strip summary.diffs from user messages — the TUI never reads them
// and they can carry multi-MB before/after file content strings.
function strip(msg: Message): Message {
if (msg.role !== "user" || !msg.summary?.diffs) return msg
const { summary, ...rest } = msg
return { ...rest, summary: { ...summary, diffs: [] } } as Message
}

sdk.event.listen((e) => {
const event = e.details
switch (event.type) {
Expand Down Expand Up @@ -199,19 +225,25 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
break

case "session.diff":
setStore("session_diff", event.properties.sessionID, event.properties.diff)
setStore(
"session_diff",
event.properties.sessionID,
event.properties.diff.map(({ before: _, after: __, ...rest }) => rest),
)
break

case "session.deleted": {
const result = Binary.search(store.session, event.properties.info.id, (s) => s.id)
if (result.found) {
const sid = event.properties.info.id
const match = Binary.search(store.session, sid, (s) => s.id)
if (match.found) {
setStore(
"session",
produce((draft) => {
draft.splice(result.index, 1)
draft.splice(match.index, 1)
}),
)
}
evict(sid)
break
}
case "session.updated": {
Expand All @@ -235,30 +267,31 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
}

case "message.updated": {
const messages = store.message[event.properties.info.sessionID]
const info = strip(event.properties.info)
const messages = store.message[info.sessionID]
if (!messages) {
setStore("message", event.properties.info.sessionID, [event.properties.info])
setStore("message", info.sessionID, [info])
break
}
const result = Binary.search(messages, event.properties.info.id, (m) => m.id)
const result = Binary.search(messages, info.id, (m) => m.id)
if (result.found) {
setStore("message", event.properties.info.sessionID, result.index, reconcile(event.properties.info))
setStore("message", info.sessionID, result.index, reconcile(info))
break
}
setStore(
"message",
event.properties.info.sessionID,
info.sessionID,
produce((draft) => {
draft.splice(result.index, 0, event.properties.info)
draft.splice(result.index, 0, info)
}),
)
const updated = store.message[event.properties.info.sessionID]
const updated = store.message[info.sessionID]
if (updated.length > 100) {
const oldest = updated[0]
batch(() => {
setStore(
"message",
event.properties.info.sessionID,
info.sessionID,
produce((draft) => {
draft.shift()
}),
Expand Down Expand Up @@ -441,7 +474,6 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
bootstrap()
})

const fullSyncedSessions = new Set<string>()
const result = {
data: store,
set: setStore,
Expand Down Expand Up @@ -481,15 +513,16 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
if (match.found) draft.session[match.index] = session.data!
if (!match.found) draft.session.splice(match.index, 0, session.data!)
draft.todo[sessionID] = todo.data ?? []
draft.message[sessionID] = messages.data!.map((x) => x.info)
draft.message[sessionID] = messages.data!.map((x) => strip(x.info))
for (const message of messages.data!) {
draft.part[message.info.id] = message.parts
}
draft.session_diff[sessionID] = diff.data ?? []
draft.session_diff[sessionID] = (diff.data ?? []).map(({ before: _, after: __, ...rest }) => rest)
}),
)
fullSyncedSessions.add(sessionID)
},
evict,
},
workspace: {
get(workspaceID: string) {
Expand Down
90 changes: 81 additions & 9 deletions packages/opencode/src/cli/cmd/tui/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ declare global {

type RpcClient = ReturnType<typeof Rpc.client<typeof rpc>>

function createWorkerFetch(client: RpcClient): typeof fetch {
// kilocode_change start — getter-indirection so fetch/events follow worker restarts
function createWorkerFetch(getter: () => RpcClient): typeof fetch {
const fn = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
const request = new Request(input, init)
const body = request.body ? await request.text() : undefined
const result = await client.call("fetch", {
const result = await getter().call("fetch", {
url: request.url,
method: request.method,
headers: Object.fromEntries(request.headers.entries()),
Expand All @@ -42,14 +43,36 @@ function createWorkerFetch(client: RpcClient): typeof fetch {
return fn as typeof fetch
}

function createEventSource(client: RpcClient): EventSource {
function createEventSource(getter: () => RpcClient): EventSource & { rebind(): void } {
// Handlers registered via on() persist across worker restarts.
// Call rebind() after replacing the RPC client so they re-attach.
const handlers = new Set<(event: Event) => void>()
let unsubs: (() => void)[] = []

function rebind() {
for (const u of unsubs) u()
unsubs = []
const cur = getter()
for (const h of handlers) {
unsubs.push(cur.on<Event>("event", h))
}
}

return {
on: (handler) => client.on<Event>("event", handler),
on: (handler) => {
handlers.add(handler)
unsubs.push(getter().on<Event>("event", handler))
return () => {
Comment thread
alex-alecu marked this conversation as resolved.
Resolved
handlers.delete(handler)
}
},
setWorkspace: (workspaceID) => {
void client.call("setWorkspace", { workspaceID })
void getter().call("setWorkspace", { workspaceID })
},
rebind,
}
}
// kilocode_change end

async function target() {
if (typeof KILO_WORKER_PATH !== "undefined") return KILO_WORKER_PATH
Expand Down Expand Up @@ -147,7 +170,10 @@ export const TuiThreadCommand = cmd({
}
const cwd = Filesystem.resolve(process.cwd())

const worker = new Worker(file, {
// kilocode_change start — mutable worker/client so /new can recycle the worker
// to reclaim native memory held by Bun's JSC allocator.
// See https://github.com/oven-sh/bun/issues/28318
let worker = new Worker(file, {
env: Object.fromEntries(
Object.entries(process.env).filter((entry): entry is [string, string] => entry[1] !== undefined),
),
Expand All @@ -156,7 +182,9 @@ export const TuiThreadCommand = cmd({
Log.Default.error(e)
}

const client = Rpc.client<typeof rpc>(worker)
let client = Rpc.client<typeof rpc>(worker)
const getClient = () => client
// kilocode_change end
const error = (e: unknown) => {
Log.Default.error(e)
}
Expand Down Expand Up @@ -185,6 +213,46 @@ export const TuiThreadCommand = cmd({
})
worker.terminate()
}

// kilocode_change start — restart the worker to reclaim native/JSC memory.
// Bun's JSC does not return freed native heap pages to the OS within a
// single Worker lifetime, so the only way to reclaim that memory after
// large sessions is to terminate the worker and spawn a fresh one.
let restarting = false
let events: ReturnType<typeof createEventSource> | undefined
const restart = async () => {
if (restarting || stopped || external) return
restarting = true
try {
Log.Default.info("restarting worker to reclaim memory")
// 1. Reject any in-flight RPC calls so callers don't hang forever.
const reason = new Error("worker restarting")
client.rejectAll(reason)
// 2. Graceful shutdown of the old worker (flushes event stream, disposes instances).
await withTimeout(client.call("shutdown", undefined), 5000).catch((err) => {
Log.Default.warn("worker shutdown during restart failed", {
error: err instanceof Error ? err.message : String(err),
})
})
worker.terminate()
// 3. Spawn fresh worker + RPC client.
worker = new Worker(file, {
env: Object.fromEntries(
Object.entries(process.env).filter((entry): entry is [string, string] => entry[1] !== undefined),
),
})
worker.onerror = (e) => {
Log.Default.error(e)
}
client = Rpc.client<typeof rpc>(worker)
// 4. Re-attach event handlers to the new client so the TUI receives
// events (including server.instance.disposed which triggers bootstrap).
events?.rebind()
Comment thread
alex-alecu marked this conversation as resolved.
Resolved
} finally {
restarting = false
}
}
// kilocode_change end
// kilocode_change start - graceful shutdown on external signals
// The worker's postMessage for the RPC result may never be delivered
// after shutdown because the worker's event loop drains. Send the
Expand Down Expand Up @@ -265,6 +333,8 @@ export const TuiThreadCommand = cmd({
network.port !== 0 ||
network.hostname !== "127.0.0.1"

// kilocode_change start — assign events so restart() can call rebind()
events = external ? undefined : createEventSource(getClient)
const transport = external
? {
url: (await client.call("server", network)).url,
Expand All @@ -273,9 +343,10 @@ export const TuiThreadCommand = cmd({
}
: {
url: "http://kilo.internal",
fetch: createWorkerFetch(client),
events: createEventSource(client),
fetch: createWorkerFetch(getClient),
events,
}
// kilocode_change end

setTimeout(() => {
client.call("checkUpgrade", { directory: cwd }).catch(() => {})
Expand Down Expand Up @@ -307,6 +378,7 @@ export const TuiThreadCommand = cmd({
directory: cwd,
fetch: transport.fetch,
events: transport.events,
restart: external ? undefined : restart, // kilocode_change — worker restart for /new
args: {
continue: args.continue,
sessionID: args.session,
Expand Down
8 changes: 6 additions & 2 deletions packages/opencode/src/session/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,19 @@ export namespace SessionSummary {
}),
async (input) => {
const diffs = await Storage.read<Snapshot.FileDiff[]>(["session_diff", input.sessionID]).catch(() => [])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Existing oversized session_diff files still load fully before scrub

Storage.read() deserializes the whole JSON blob here, so a previously persisted multi-GB before/after payload is already in memory before the limit check runs. That means the "clean on first load" migration still hits the same first-load memory spike/OOM this PR is trying to prevent; only later reads benefit after the rewrite succeeds.

const limit = 256 * 1024

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you not use the constant here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 664ceec. Exported the constant from Snapshot and reused it here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 664ceec (reuses Snapshot.MAX_DIFF_SIZE) and 1cb4b5c (broader cleanup: replaced fs.readFile with Bun.file, simplified resolveVariant params, removed destructuring in strip(), added .catch() to config read, replaced push loop with .filter(), removed if/else).

const next = diffs.map((item) => {
const file = unquoteGitPath(item.file)
if (file === item.file) return item
const oversized = Buffer.byteLength(item.before) > limit || Buffer.byteLength(item.after) > limit
if (file === item.file && !oversized) return item
return {
...item,
file,
before: oversized ? "" : item.before,
after: oversized ? "" : item.after,
}
})
const changed = next.some((item, i) => item.file !== diffs[i]?.file)
const changed = next.some((item, i) => item !== diffs[i])
if (changed) Storage.write(["session_diff", input.sessionID], next).catch(() => {})
return next
},
Expand Down
Loading
Loading