Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 7 additions & 5 deletions packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "jsonc-parser"
// kilocode_change end
import { Instance } from "../project/instance"
import { State } from "../project/state" // kilocode_change
import { LSPServer } from "../lsp/server"
import { BunProc } from "@/bun"
import { Installation } from "@/installation"
Expand Down Expand Up @@ -1553,11 +1554,12 @@ export namespace Config {

global.reset()

// kilocode_change start - only reset config cache, don't dispose all instances.
// Instance.disposeAll() was destroying all session state, MCP connections, and
// in-flight operations across every project whenever any global config changed
// (e.g. removing a mode). The cache reset above is sufficient — consumers will
// pick up the new config on their next read.
// kilocode_change start - reset all derived caches (Config.state, Agent.state,
// Provider.state, etc.) so they re-read the updated global config on next access.
// Only cache entries without dispose callbacks are cleared — side-effectful state
// like sessions, MCP connections, and file watchers are preserved.
State.resetCaches()

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.

WARNING: Config-backed services with disposers will not reload

State.resetCaches() only invalidates entries without a dispose callback. Services like MCP.state, LSP.state, and FileWatcher.state read Config.get() during initialization and do have disposers, so they keep running with the old global config. Changes like enabling/disabling MCP or LSP servers, or updating watcher ignore rules, will not take effect until the instance is fully disposed.


GlobalBus.emit("event", {
directory: "global",
payload: {
Expand Down
15 changes: 15 additions & 0 deletions packages/opencode/src/project/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ export namespace State {
}
}

/**
* Reset all cache-only entries (those without dispose callbacks) across all
* instances. This invalidates derived state like Config, Agent, Provider,
* etc. without tearing down side-effectful resources (MCP connections,
* sessions, file watchers, etc.).
*/
export function resetCaches() {
for (const entries of recordsByKey.values()) {
for (const [init, entry] of entries) {
if (entry.dispose) continue

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.

WARNING: dispose is not a safe proxy for cache-only state

This also deletes long-lived per-instance stores that intentionally have no disposer, including SessionStatus.state, FileTime.state, InstructionPrompt.state, Bus.state, and Env.state. After a global config update, active sessions can lose busy status, read/write locks, subscriptions, and env overrides even though this change is meant to preserve session state.

entries.delete(init)
}
}
}

export async function dispose(key: string) {
const entries = recordsByKey.get(key)
if (!entries) return
Expand Down
Loading