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
@@ -1,8 +1,16 @@
import React from 'react';
import { toast } from '@/components/ui';
import { useInlineCommentDraftStore, type InlineCommentDraft, type InlineCommentSource } from '@/stores/useInlineCommentDraftStore';
import {
EMPTY_INLINE_COMMENT_DRAFTS,
getInlineCommentDraftKey,
useInlineCommentDraftStore,
type InlineCommentDraft,
type InlineCommentSource,
} from '@/stores/useInlineCommentDraftStore';
import { useSessionUIStore } from '@/sync/session-ui-store';
import { useI18n } from '@/lib/i18n';
import { useEffectiveDirectory } from '@/hooks/useEffectiveDirectory';
import { getRuntimeKey } from '@/lib/runtime-switch';
type LineRangeBase = {
start: number;
@@ -52,25 +60,42 @@ export function useInlineCommentController<TRange extends LineRangeBase>(
const currentSessionId = useSessionUIStore((state) => state.currentSessionId);
const newSessionDraftOpen = useSessionUIStore((state) => state.newSessionDraft?.open);
const effectiveDirectory = useEffectiveDirectory();
const sessionDirectory = useSessionUIStore(
React.useCallback(
(state) => currentSessionId ? state.getDirectoryForSession(currentSessionId) : null,
[currentSessionId],
),
);
const addDraft = useInlineCommentDraftStore((state) => state.addDraft);
const updateDraft = useInlineCommentDraftStore((state) => state.updateDraft);
const removeDraft = useInlineCommentDraftStore((state) => state.removeDraft);
const allDrafts = useInlineCommentDraftStore((state) => state.drafts);
const [selection, setSelection] = React.useState<TRange | null>(null);
const [commentText, setCommentText] = React.useState('');
const [editingDraftId, setEditingDraftId] = React.useState<string | null>(null);
const sessionKey = React.useMemo(() => {
return currentSessionId ?? (newSessionDraftOpen ? 'draft' : null);
}, [currentSessionId, newSessionDraftOpen]);
const sessionKey = currentSessionId ?? (newSessionDraftOpen ? 'draft' : null);
const draftDirectory = sessionDirectory ?? effectiveDirectory;
const target = React.useMemo(() => {
if (!sessionKey || !draftDirectory) return null;
return { directory: draftDirectory, sessionKey };
}, [draftDirectory, sessionKey]);
const targetKey = target
? getInlineCommentDraftKey(getRuntimeKey(), target.directory, target.sessionKey)
: null;
const sessionDrafts = useInlineCommentDraftStore(
React.useCallback(
(state) => targetKey ? state.drafts[targetKey] ?? EMPTY_INLINE_COMMENT_DRAFTS : EMPTY_INLINE_COMMENT_DRAFTS,
[targetKey],
),
);
const drafts = React.useMemo(() => {
if (!sessionKey || !fileLabel) return [];
const sessionDrafts = allDrafts[sessionKey] ?? [];
if (!target || !fileLabel) return [];
return sessionDrafts.filter((draft) => draft.source === source && draft.fileLabel === fileLabel);
}, [allDrafts, fileLabel, sessionKey, source]);
}, [fileLabel, sessionDrafts, source, target]);
const reset = React.useCallback(() => {
setSelection(null);
@@ -90,18 +115,19 @@ export function useInlineCommentController<TRange extends LineRangeBase>(
}, [fromDraftRange]);
const deleteDraft = React.useCallback((draft: InlineCommentDraft) => {
removeDraft(draft.sessionKey, draft.id);
if (!target) return;
removeDraft(target, draft.id);
if (editingDraftId === draft.id) {
reset();
}
}, [editingDraftId, removeDraft, reset]);
}, [editingDraftId, removeDraft, reset, target]);
const saveComment = React.useCallback((textToSave: string, rangeOverride?: TRange) => {
const targetRange = rangeOverride ?? selection;
const trimmedText = textToSave.trim();
if (!targetRange || !trimmedText || !fileLabel) return;
if (!sessionKey) {
if (!target) {
toast.error(t('inlineComment.toast.selectSessionToSave'));
return;
}
@@ -111,7 +137,7 @@ export function useInlineCommentController<TRange extends LineRangeBase>(
const code = getCodeForRange(normalizedRange);
if (editingDraftId) {
updateDraft(sessionKey, editingDraftId, {
updateDraft(target, editingDraftId, {
fileLabel,
startLine: normalizedStoreRange.startLine,
endLine: normalizedStoreRange.endLine,
@@ -121,8 +147,7 @@ export function useInlineCommentController<TRange extends LineRangeBase>(
text: trimmedText,
});
} else {
addDraft({
sessionKey,
addDraft(target, {
source,
fileLabel,
startLine: normalizedStoreRange.startLine,
@@ -135,7 +160,7 @@ export function useInlineCommentController<TRange extends LineRangeBase>(
}
reset();
}, [addDraft, editingDraftId, fileLabel, getCodeForRange, language, reset, selection, sessionKey, source, t, toStoreRange, updateDraft]);
}, [addDraft, editingDraftId, fileLabel, getCodeForRange, language, reset, selection, source, t, target, toStoreRange, updateDraft]);
return {
sessionKey,