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
@@ -0,0 +1,78 @@
import { beforeEach, describe, expect, test } from 'bun:test';
import type { Todo } from '@opencode-ai/sdk/v2/client';
import { getRuntimeKey } from '@/lib/runtime-switch';
import { createChatDraftIdentity, readChatDraft, writeChatDraft } from '@/lib/chatDraftPersistence';
import { createMessageQueueTarget, useMessageQueueStore } from '@/stores/messageQueueStore';
import { useSessionFoldersStore } from '@/stores/useSessionFoldersStore';
import { useTodosPersistStore } from '@/stores/useTodosPersistStore';
import { useInlineCommentDraftStore } from '@/stores/useInlineCommentDraftStore';
import { isSessionPinned, useSessionPinnedStore } from '@/stores/useSessionPinnedStore';
import { cleanupPersistedSessionState } from './session-deletion-cleanup';
const todo: Todo = { content: 'persisted', status: 'pending', priority: 'medium' };
describe('cleanupPersistedSessionState', () => {
beforeEach(() => {
useMessageQueueStore.setState({ queuedMessages: {}, quarantinedLegacyMessages: {} });
useTodosPersistStore.setState({ sessions: {} });
useInlineCommentDraftStore.setState({ drafts: {}, touchedAt: {} });
useSessionPinnedStore.setState({ ids: new Set(), touchedAt: {} });
useSessionFoldersStore.setState({ foldersMap: {}, collapsedFolderIds: new Set() });
});
test('clears queue and todos only for the deleted composite session', () => {
const runtimeKey = getRuntimeKey();
const deleted = createMessageQueueTarget('session-1', '/repo-a', runtimeKey)!;
const retained = createMessageQueueTarget('session-1', '/repo-b', runtimeKey)!;
useMessageQueueStore.getState().addToQueue(deleted, { content: 'delete' });
useMessageQueueStore.getState().addToQueue(retained, { content: 'retain' });
useTodosPersistStore.getState().setSessionTodos('/repo-a', 'session-1', [todo]);
useTodosPersistStore.getState().setSessionTodos('/repo-b', 'session-1', [todo]);
const deletedDraft = createChatDraftIdentity(runtimeKey, '/repo-a', 'session-1')!;
const retainedDraft = createChatDraftIdentity(runtimeKey, '/repo-b', 'session-1')!;
writeChatDraft(deletedDraft, 'delete', []);
writeChatDraft(retainedDraft, 'retain', []);
const inlineDraft = {
source: 'terminal' as const,
fileLabel: 'Terminal',
startLine: 1,
endLine: 1,
code: 'context',
language: 'text',
text: '',
};
useInlineCommentDraftStore.getState().addDraft({ directory: '/repo-a', sessionKey: 'session-1' }, inlineDraft);
useInlineCommentDraftStore.getState().addDraft({ directory: '/repo-b', sessionKey: 'session-1' }, inlineDraft);
useSessionPinnedStore.getState().toggle({ directory: '/repo-a', sessionId: 'session-1' });
useSessionPinnedStore.getState().toggle({ directory: '/repo-b', sessionId: 'session-1' });
const folder = useSessionFoldersStore.getState().createFolder('/repo-a', 'Active');
useSessionFoldersStore.getState().addSessionToFolder('/repo-a', folder.id, 'session-1');
const archivedFolder = useSessionFoldersStore.getState().createFolder('__archived__:/repo-a', 'Archived');
useSessionFoldersStore.getState().addSessionToFolder('__archived__:/repo-a', archivedFolder.id, 'session-1');
cleanupPersistedSessionState({ runtimeKey, directory: '/repo-a', sessionId: 'session-1' });
expect(useMessageQueueStore.getState().getQueueForTarget(deleted)).toEqual([]);
expect(useMessageQueueStore.getState().getQueueForTarget(retained)).toHaveLength(1);
expect(useTodosPersistStore.getState().getSessionTodos('/repo-a', 'session-1')).toBe(undefined);
expect(useTodosPersistStore.getState().getSessionTodos('/repo-b', 'session-1')).toEqual([todo]);
expect(readChatDraft(deletedDraft).text).toBe('');
expect(readChatDraft(retainedDraft).text).toBe('retain');
expect(useInlineCommentDraftStore.getState().getDrafts({ directory: '/repo-a', sessionKey: 'session-1' })).toEqual([]);
expect(useInlineCommentDraftStore.getState().getDrafts({ directory: '/repo-b', sessionKey: 'session-1' })).toHaveLength(1);
expect(isSessionPinned(useSessionPinnedStore.getState().ids, '/repo-a', 'session-1')).toBe(false);
expect(isSessionPinned(useSessionPinnedStore.getState().ids, '/repo-b', 'session-1')).toBe(true);
expect(useSessionFoldersStore.getState().getSessionFolderId('/repo-a', 'session-1')).toBeNull();
expect(useSessionFoldersStore.getState().getSessionFolderId('__archived__:/repo-a', 'session-1')).toBeNull();
});
test('rejects stale runtime cleanup', () => {
const runtimeKey = getRuntimeKey();
useTodosPersistStore.getState().setSessionTodos('/repo', 'session-1', [todo]);
cleanupPersistedSessionState({ runtimeKey: `${runtimeKey}-stale`, directory: '/repo', sessionId: 'session-1' });
expect(useTodosPersistStore.getState().getSessionTodos('/repo', 'session-1')).toEqual([todo]);
});
});