perf: overhaul session loading, caching, and runtime isolation (#2360)

Improve OpenChamber responsiveness under large session workloads while fixing
cache, synchronization, and persistence correctness across runtimes, projects,
directories, and worktrees.

- prioritize selected and visible sessions during bootstrap and defer
  non-critical enrichment work
- reduce redundant message loading, event processing, store publication, and
  hidden sidebar work
- prevent stale session and message requests from overwriting newer
  authoritative state
- preserve existing data when authoritative fetches fail instead of treating
  failures as successful empty responses
- scope session materialization, messages, drafts, queues, todos, pins,
  permissions, folders, tabs, Git state, and pull request data by runtime and
  directory identity
- harden runtime switching, reconnect, cleanup, mutation reconciliation, and
  persisted-state ordering
- preserve live subagent Task linkage when metadata arrives after an older
  message request or while streaming parts are suspended
- coalesce overlapping tail refreshes without losing newer refresh demand
- improve cold-session loading by moving deferrable work out of the critical
  bootstrap path
- isolate URL authentication, mobile credentials, native secrets, and other
  runtime-owned state across endpoint changes
- bound long-lived caches and remove avoidable allocations from event and
  rendering hot paths
- limit virtualization to archive collections where it improves rendering
  without disrupting active sidebar layout
- stabilize session folders, pin ordering, expanded state, and persisted
  sidebar behavior
- open skill files through the same secure editor and outside-workspace grant
  flow used by file navigation, including worktree sessions
- expand regression coverage for stale completions, runtime collisions,
  reconnect behavior, persistence races, authoritative empty results, and
  subagent refresh ordering
- document the updated synchronization, cache ownership, performance, and
  runtime-isolation invariants
This commit is contained in:
Bohdan Triapitsyn
2026-07-21 20:52:20 +03:00
committed by GitHub
parent 485efc7117
commit 85400459e9
197 changed files with 10835 additions and 3400 deletions
+39 -1
View File
@@ -1,5 +1,6 @@
import type { Message, Part } from "@opencode-ai/sdk/v2/client"
import { mergeMessages } from "./optimistic"
import type { SessionMaterializationReason } from "./event-reducer"
const cmp = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0)
const STREAMING_PART_FIELDS = ["text", "output"] as const
@@ -33,6 +34,40 @@ export type SessionMaterializationStatus = {
missingPartMessageIDs: string[]
}
export type SessionMaterializationRequest = {
reason: SessionMaterializationReason
messageID?: string
partID?: string
}
export const getSessionMaterializationRequestKey = (
runtimeKey: string,
directory: string,
sessionID: string,
): string => JSON.stringify([runtimeKey, directory, sessionID])
export function isSessionMaterializationStillNeeded(
state: MaterializedState,
sessionID: string,
request: SessionMaterializationRequest,
): boolean {
if (request.reason === "empty-assistant-message") {
return !request.messageID || !Object.prototype.hasOwnProperty.call(state.part, request.messageID)
}
if (request.reason === "missing-owning-message") {
if (!request.messageID) return true
return !(state.message[sessionID] ?? []).some((message) => message.id === request.messageID)
}
if (request.reason === "orphan-delta" || request.reason === "missing-delta-part") {
if (!request.messageID || !request.partID) return true
return !(state.part[request.messageID] ?? []).some((part) => part.id === request.partID)
}
return true
}
function sortParts(parts: Part[], skipPartTypes: ReadonlySet<string>) {
return parts
.filter((part) => !!part?.id && !skipPartTypes.has(part.type))
@@ -50,6 +85,7 @@ function haveEquivalentPartSnapshots(left: Part[] | undefined, right: Part[]): b
const leftPart = left[index]
const rightPart = right[index]
if (!leftPart || !rightPart) return false
if (leftPart === rightPart) continue
if (leftPart.id !== rightPart.id) return false
if (JSON.stringify(leftPart) !== JSON.stringify(rightPart)) return false
}
@@ -171,7 +207,7 @@ export function materializeSessionSnapshots(
const messagesChanged = messages !== currentMessages || (existingMessages === undefined && snapshots.length === 0)
let partsChanged = false
const nextPartState = { ...state.part }
let nextPartState = state.part
const isPrepend = options.mode === "prepend"
for (const record of snapshots) {
@@ -194,6 +230,8 @@ export function materializeSessionSnapshots(
: nextParts.length === 0 && !isAssistant
if (equivalent) continue
if (nextPartState === state.part) nextPartState = { ...state.part }
if (nextParts.length === 0 && !isAssistant) {
delete nextPartState[messageID]
} else {