fix: show multi-run sessions immediately

Register created multi-run sessions in sidebar state
Preserve reduced startup refresh fanout
Add regression coverage
This commit is contained in:
Bohdan Triapitsyn
2026-05-25 17:37:27 +03:00
parent f585ca2f60
commit 25ed8e6abb
2 changed files with 192 additions and 0 deletions
@@ -1,4 +1,5 @@
import { create } from 'zustand';
import type { Session } from '@opencode-ai/sdk/v2';
import { routeMessage, useSessionUIStore } from '@/sync/session-ui-store';
import { devtools } from 'zustand/middleware';
import type { CreateMultiRunParams, CreateMultiRunResult } from '@/types/multirun';
@@ -11,7 +12,9 @@ import { checkIsGitRepository } from '@/lib/gitApi';
import { useDirectoryStore } from './useDirectoryStore';
import { useProjectsStore } from './useProjectsStore';
import { useSnippetsStore } from './useSnippetsStore';
import { useGlobalSessionsStore } from './useGlobalSessionsStore';
import { getMultiRunSessionTitle } from '@/lib/multirun/title';
import { getSyncChildStores, registerSessionDirectory } from '@/sync/sync-refs';
const toGitSafeSlug = (value: string): string => {
return value
@@ -31,6 +34,51 @@ const generateWorktreeNameSeed = (groupSlug: string, modelSlug: string): string
return `${groupSlug}/${modelSlug}`;
};
const normalizePath = (value: string): string => {
const replaced = value.replace(/\\/g, '/');
if (replaced === '/') {
return '/';
}
return replaced.length > 1 ? replaced.replace(/\/+$/, '') : replaced;
};
const registerCreatedSession = (session: Session, directory: string): Session => {
const normalizedDirectory = normalizePath(directory);
const sessionDirectory = (session as Session & { directory?: string | null }).directory;
const sessionWithDirectory = typeof sessionDirectory === 'string' && sessionDirectory.trim().length > 0
? session
: ({ ...session, directory: normalizedDirectory } as Session);
registerSessionDirectory(session.id, normalizedDirectory);
useSessionUIStore.getState().markSessionAsOpenChamberCreated(session.id);
useGlobalSessionsStore.getState().upsertSession(sessionWithDirectory);
try {
const store = getSyncChildStores().ensureChild(normalizedDirectory, { bootstrap: false });
store.setState((state) => {
const existingIndex = state.session.findIndex((candidate) => candidate.id === session.id);
if (existingIndex >= 0 && state.session[existingIndex] === sessionWithDirectory) {
return state;
}
const nextSessions = existingIndex >= 0
? state.session.map((candidate, index) => index === existingIndex ? sessionWithDirectory : candidate)
: [...state.session, sessionWithDirectory].sort((a, b) => a.id.localeCompare(b.id));
return {
session: nextSessions,
sessionTotal: Math.max(state.sessionTotal, nextSessions.length),
limit: Math.max(state.limit, nextSessions.length),
};
});
} catch {
// SyncProvider can be unavailable in tests or detached surfaces; the global
// session upsert above is enough for the sidebar to show the session.
}
return sessionWithDirectory;
};
const resolveActiveProject = (): ProjectRef | null => {
const projectsState = useProjectsStore.getState();
const activeProjectId = projectsState.activeProjectId;
@@ -165,6 +213,7 @@ export const useMultiRunStore = create<MultiRunStore>()(
directory,
() => opencodeClient.createSession({ title: sessionTitle }),
);
registerCreatedSession(session, directory);
createdRuns.push({
sessionId: session.id,
@@ -198,6 +247,7 @@ export const useMultiRunStore = create<MultiRunStore>()(
worktreeMetadata.path,
() => opencodeClient.createSession({ title: sessionTitle }),
);
registerCreatedSession(session, worktreeMetadata.path);
useSessionUIStore.getState().setWorktreeMetadata(session.id, enrichedMetadata);