Skip to content
Merged
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
33 changes: 33 additions & 0 deletions apps/desktop/src/main/__tests__/workbar-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { createSessionCatalogController, selectAuthoritativeSessionIds } from '../../renderer/application/contracts/session-catalog/session-catalog-state.js';
import { sessionIdSetsEqual } from '../../renderer/features/conversation/index.js';
import type { DesktopSessionSummary } from '../../shared/desktop-session-projection.js';
import assert from 'node:assert/strict';
import { afterEach, describe, it } from 'node:test';
import {
Expand Down Expand Up @@ -386,6 +387,38 @@ describe('Workbar topology', () => {
assert.equal(sessionIdSetsEqual(empty, new Set()), true);
});

it('keeps other Sessions\' visibility when a row patch lands before the first list', () => {
cleanups.push(installMemoryLocalStorage({
'maka-session-workbar-collapsed-v2': JSON.stringify({ owner: false, running: false }),
}));
const row = (id: string) =>
({ id, name: id, activityAt: 1, isArchived: false, revision: 1 }) as DesktopSessionSummary;
const catalog = createSessionCatalogController();
let state = loadWorkbarLayout(undefined);
// Mirrors useWorkbarLayoutState: retain whenever the catalog is authoritative.
const retain = () => {
const sessionIds = selectAuthoritativeSessionIds(catalog.getState());
if (sessionIds) state = reduceWorkbarLayout(state, { type: 'retain-sessions', sessionIds });
};
// After a reload, a still-running Session's change event can be read
// before the startup list; that row alone says nothing about the others.
catalog.commitPatch('running', row('running'));
retain();
assert.equal(selectAuthoritativeSessionIds(catalog.getState()), undefined);
catalog.commitSessions([row('owner'), row('running')]);
retain();
persistWorkbarLayout(state, 'right-visibility');
assert.equal(isSessionWorkbarCollapsed(loadWorkbarLayout('owner')), false);
});

it('treats a first list that matches the patched rows as authoritative', () => {
const row = { id: 'a', name: 'a', activityAt: 1, isArchived: false, revision: 1 } as DesktopSessionSummary;
const catalog = createSessionCatalogController();
catalog.commitPatch('a', row);
catalog.commitSessions([row]);
assert.deepEqual(selectAuthoritativeSessionIds(catalog.getState()), new Set(['a']));
});

it('evicts deleted Sessions without dropping an active Session awaiting catalog hydration', () => {
cleanups.push(installMemoryLocalStorage({
'maka-session-workbar-collapsed-v2': JSON.stringify({ a: false, b: false, deleted: false }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ import { createObservableState } from './observable-state.js';
export interface SessionCatalogState {
readonly sessions: readonly DesktopSessionSummary[];
readonly revision: number;
/**
* A list read has committed. A row patch also moves `revision`, but it can
* only vouch for its own row, so membership readers wait for a list.
*/
readonly listed: boolean;
readonly activeSessionId: string | undefined;
readonly automaticQueryBlockedSessionIds: ReadonlySet<string>;
/**
Expand Down Expand Up @@ -76,6 +81,7 @@ export function createSessionCatalogController(source: SessionCatalogSource = DE
const state = createObservableState<SessionCatalogState>({
sessions: [],
revision: 0,
listed: false,
activeSessionId: undefined,
automaticQueryBlockedSessionIds: new Set(),
removedIds: new Set(),
Expand Down Expand Up @@ -169,14 +175,15 @@ export function createSessionCatalogController(source: SessionCatalogSource = DE
const sameRows = sessions.length === current.sessions.length
&& sessions.every((s, i) => s === current.sessions[i]);
// A commit that changed nothing publishes nothing — except the first
// one: revision 0 means "no authoritative observation yet", and even an
// empty list is one.
if (sameRows && removedIds === current.removedIds && current.revision > 0) return;
// list: until one lands there is no authoritative observation, and even
// an empty list, or one matching the rows patches admitted, is one.
if (sameRows && removedIds === current.removedIds && current.listed) return;
state.replaceState({
...current,
sessions: sameRows ? current.sessions : sessions,
removedIds,
revision: current.revision + 1,
listed: true,
});
},
commitPatch(sessionId: string, summary: DesktopSessionSummary | null): void {
Expand Down Expand Up @@ -303,8 +310,9 @@ export const selectActiveSessionId = (state: SessionCatalogState): string | unde
export const selectAuthoritativeSessionIds = (
state: SessionCatalogState,
): ReadonlySet<string> | undefined =>
// The initial empty catalog cannot prove that persisted Sessions were deleted.
state.revision > 0 ? new Set(state.sessions.map(({ id }) => id)) : undefined;
// Neither the initial empty catalog nor rows admitted by targeted patches
// before the first list can prove that persisted Sessions were deleted.
state.listed ? new Set(state.sessions.map(({ id }) => id)) : undefined;

/**
* The shell's catalog instance, mounted once above the feature services.
Expand Down