Skip to content

Commit 1c7c510

Browse files
author
Peter Steinberger
committed
fix(ui): preserve scoped session state across updates
1 parent 12297b7 commit 1c7c510

12 files changed

Lines changed: 758 additions & 65 deletions
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { asNullableRecord } from "@openclaw/normalization-core/record-coerce";
2+
import type { RouteId } from "../app-route-paths.ts";
3+
import type { ApplicationContext } from "../app/context.ts";
4+
import { readPresenceEntries, type PresencePayload } from "../app/user-profile.ts";
5+
import { createSessionEventRefreshCoordinator } from "../lib/sessions/event-refresh-coordinator.ts";
6+
import { readSessionChangedEvent } from "../lib/sessions/reconcile.ts";
7+
import { normalizeAgentId, parseAgentSessionKey } from "../lib/sessions/session-key.ts";
8+
import type { SidebarSessionStatusFilter } from "./app-sidebar-session-types.ts";
9+
import type { SessionDataControllerHost } from "./session-data-controller-catalog.ts";
10+
11+
type SessionGatewayEventOwner = {
12+
presencePayload: PresencePayload | undefined;
13+
readonly sessionScopeGeneration: number;
14+
handleSessionCatalogHostEvent(payload: unknown): void;
15+
handleSessionCatalogPresence(payload: unknown): void;
16+
refreshSidebarSessions(agentId?: string): Promise<void>;
17+
requestSessionDataUpdate(): void;
18+
};
19+
20+
type FilteredSessionRefreshScope = {
21+
agentId: string;
22+
archivedFilter: SidebarSessionStatusFilter;
23+
client: ApplicationContext<RouteId>["gateway"]["snapshot"]["client"];
24+
generation: number;
25+
};
26+
27+
export function subscribeSessionDataGatewayEvents(
28+
gateway: ApplicationContext<RouteId>["gateway"],
29+
owner: SessionGatewayEventOwner,
30+
host: Pick<
31+
SessionDataControllerHost,
32+
"expandedAgentId" | "isConnected" | "sidebarSessionStatusFilter"
33+
>,
34+
): () => void {
35+
let subscribed = true;
36+
let refreshScope: FilteredSessionRefreshScope | null = null;
37+
const scopeIsCurrent = () =>
38+
refreshScope !== null &&
39+
subscribed &&
40+
host.isConnected &&
41+
gateway.snapshot.phase === "connected" &&
42+
gateway.snapshot.client === refreshScope.client &&
43+
owner.sessionScopeGeneration === refreshScope.generation &&
44+
host.sidebarSessionStatusFilter() === refreshScope.archivedFilter &&
45+
normalizeAgentId(host.expandedAgentId()) === refreshScope.agentId;
46+
const refreshCoordinator = createSessionEventRefreshCoordinator({
47+
canRefresh: scopeIsCurrent,
48+
refresh: () =>
49+
refreshScope ? owner.refreshSidebarSessions(refreshScope.agentId) : Promise.resolve(),
50+
});
51+
const unsubscribe = gateway.subscribeEvents((event) => {
52+
if (event.event === "sessions.catalog.host") {
53+
owner.handleSessionCatalogHostEvent(event.payload);
54+
return;
55+
}
56+
if (event.event === "sessions.changed") {
57+
const archivedFilter = host.sidebarSessionStatusFilter();
58+
if (archivedFilter === "active") {
59+
return;
60+
}
61+
const agentId = normalizeAgentId(host.expandedAgentId());
62+
const sessionEvent = readSessionChangedEvent(event.payload);
63+
const payloadAgentId = asNullableRecord(event.payload)?.agentId;
64+
const eventAgentId =
65+
sessionEvent?.agentId ??
66+
parseAgentSessionKey(sessionEvent?.key)?.agentId ??
67+
(typeof payloadAgentId === "string" ? payloadAgentId : undefined);
68+
if (eventAgentId && normalizeAgentId(eventAgentId) !== agentId) {
69+
return;
70+
}
71+
const nextScope: FilteredSessionRefreshScope = {
72+
agentId,
73+
archivedFilter,
74+
client: gateway.snapshot.client,
75+
generation: owner.sessionScopeGeneration,
76+
};
77+
if (
78+
refreshScope &&
79+
(refreshScope.agentId !== nextScope.agentId ||
80+
refreshScope.archivedFilter !== nextScope.archivedFilter ||
81+
refreshScope.client !== nextScope.client ||
82+
refreshScope.generation !== nextScope.generation)
83+
) {
84+
refreshCoordinator.reset();
85+
}
86+
refreshScope = nextScope;
87+
// Canonical debounce/max-wait and single-flight behavior belong to one
88+
// coordinator; scope checks retire stale agents, filters, and clients.
89+
refreshCoordinator.schedule();
90+
return;
91+
}
92+
if (event.event === "presence") {
93+
const presence = readPresenceEntries(event.payload);
94+
owner.presencePayload = presence ? { presence } : undefined;
95+
owner.requestSessionDataUpdate();
96+
owner.handleSessionCatalogPresence(event.payload);
97+
}
98+
});
99+
return () => {
100+
subscribed = false;
101+
refreshCoordinator.dispose();
102+
unsubscribe();
103+
};
104+
}

‎ui/src/components/session-data-controller-pagination.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@ type SidebarSessionPaginationOwner = {
2424
export type SidebarSessionPaginationState = {
2525
listRequestToken: symbol | null;
2626
pageRequestToken: symbol | null;
27+
loadedScope?: {
28+
agentId: string;
29+
archivedFilter: SidebarSessionStatusFilter;
30+
generation: number;
31+
};
2732
};
2833

2934
function publishSidebarSessionResult(
3035
owner: SidebarSessionPaginationOwner,
3136
agentId: string,
3237
result: SessionsListResult | null,
38+
archivedFilter: SidebarSessionStatusFilter,
39+
generation: number,
3340
) {
3441
owner.sessionsResult = result;
3542
owner.sessionsAgentId = agentId;
43+
owner.sidebarSessionPaginationState.loadedScope = {
44+
agentId: normalizeAgentId(agentId),
45+
archivedFilter,
46+
generation,
47+
};
3648
if (result) {
3749
owner.sessionRowsByAgent[normalizeAgentId(agentId)] = result.sessions;
3850
for (const row of result.sessions) {
@@ -84,10 +96,19 @@ export async function refreshSidebarSessions(
8496
const state = owner.sidebarSessionPaginationState;
8597
state.pageRequestToken = null;
8698
const archivedFilter = statusFilter();
99+
const loadedScope = state.loadedScope;
100+
const preservesVisibleScope =
101+
archivedFilter !== "active" &&
102+
loadedScope?.generation === owner.sessionScopeGeneration &&
103+
loadedScope.archivedFilter === archivedFilter &&
104+
loadedScope.agentId === normalizeAgentId(agentId) &&
105+
normalizeAgentId(owner.sessionsAgentId ?? "") === loadedScope.agentId;
87106
const options = {
88107
agentId,
89108
archivedFilter,
90-
limit: SIDEBAR_AGENT_SESSION_LIST_LIMIT,
109+
limit: preservesVisibleScope
110+
? Math.max(SIDEBAR_AGENT_SESSION_LIST_LIMIT, owner.sessionsResult?.sessions.length ?? 0)
111+
: SIDEBAR_AGENT_SESSION_LIST_LIMIT,
91112
includeGlobal: true,
92113
includeUnknown: true,
93114
configuredAgentsOnly: true,
@@ -120,7 +141,7 @@ export async function refreshSidebarSessions(
120141
try {
121142
const result = await context.sessions.list(options);
122143
if (isCurrent()) {
123-
publishSidebarSessionResult(owner, agentId, result);
144+
publishSidebarSessionResult(owner, agentId, result, archivedFilter, generation);
124145
}
125146
} catch (error) {
126147
if (isCurrent()) {
@@ -204,7 +225,13 @@ export async function loadMoreSidebarSessions(
204225

205226
const page = await context.sessions.list(options);
206227
if (page && isCurrent()) {
207-
publishSidebarSessionResult(owner, agentId, appendSidebarSessionResults(previous, page));
228+
publishSidebarSessionResult(
229+
owner,
230+
agentId,
231+
appendSidebarSessionResults(previous, page),
232+
archivedFilter,
233+
generation,
234+
);
208235
}
209236
} catch (error) {
210237
if (isCurrent()) {

0 commit comments

Comments
 (0)