Reduce workspace, agent, and chat sync traffic (#2028)

* feat(sync): keep live data scoped and current

Only viewed chats receive live timeline rows, while directory state now uses one subscribed bootstrap followed by ordered deltas. Legacy clients and daemons retain their existing behavior through centralized compatibility gates.

* fix(sync): preserve selective delivery boundaries

Keep selective timeline capability app-owned, union viewed sets across shared sockets, and reconcile directory side effects from accepted state. Visibility and archive suppression now follow their existing authoritative boundaries.

* test(app): align browser fixtures with runtime contracts

* fix(sync): preserve mixed-client delivery guarantees

* fix(sync): finish paged history before advancing

* fix(sync): replay live deltas after refresh failures

* test(server): model socket capability lookup

* test(app): use platform shortcut for split-pane coverage

* fix(app): keep hidden timelines dormant

* test(app): normalize split-pane shortcut setup

* fix(app): preserve sync state across retries

* fix(app): preserve background sync state across races

* fix directory bootstrap reconciliation edge cases

* fix selective timeline compatibility races

* fix superseded directory sync races

* fix(sync): centralize directory replica ordering

Own agent and workspace refresh transactions in HostRuntime so reconnect epochs, buffered updates, and lifecycle invalidation share one ordering boundary. Route timeline metadata and mixed-capability stream delivery through their authoritative runtime sources.

* fix(app): keep timeline requests runtime-scoped

* fix(app): close directory bootstrap races
This commit is contained in:
Mohamed Boudra
2026-07-17 17:03:34 +02:00
committed by GitHub
parent 6c99efae52
commit 1977d330ed
57 changed files with 5783 additions and 1343 deletions
@@ -0,0 +1,112 @@
import { expect, test } from "vitest";
import type { WorkspaceLayout } from "@/stores/workspace-layout-store";
import type { WorkspaceTab } from "@/stores/workspace-tabs-store";
import { selectVisibleAgentIds } from "./visible-agent-ids";
test("selects only the active agent tab in every visible pane", () => {
const layout: WorkspaceLayout = {
focusedPaneId: "left",
root: {
kind: "group",
group: {
id: "root",
direction: "horizontal",
sizes: [0.5, 0.5],
children: [
{ kind: "pane", pane: { id: "left", tabIds: ["a", "hidden"], focusedTabId: "a" } },
{ kind: "pane", pane: { id: "right", tabIds: ["b"], focusedTabId: "b" } },
],
},
},
};
const tabs: WorkspaceTab[] = [
{ tabId: "a", target: { kind: "agent", agentId: "agent-a" }, createdAt: 1 },
{ tabId: "hidden", target: { kind: "agent", agentId: "agent-hidden" }, createdAt: 2 },
{ tabId: "b", target: { kind: "agent", agentId: "agent-b" }, createdAt: 3 },
];
expect(
selectVisibleAgentIds({ layout, tabs, routeFocused: true, focusedPaneOnly: false }),
).toEqual(["agent-a", "agent-b"]);
});
test("route blur publishes no viewed agents", () => {
const layout: WorkspaceLayout = {
focusedPaneId: "main",
root: { kind: "pane", pane: { id: "main", tabIds: ["a"], focusedTabId: "a" } },
};
const tabs: WorkspaceTab[] = [
{ tabId: "a", target: { kind: "agent", agentId: "agent-a" }, createdAt: 1 },
];
expect(
selectVisibleAgentIds({ layout, tabs, routeFocused: false, focusedPaneOnly: false }),
).toEqual([]);
});
test("compact and focus modes contribute only the focused pane", () => {
const layout: WorkspaceLayout = {
focusedPaneId: "right",
root: {
kind: "group",
group: {
id: "root",
direction: "horizontal",
sizes: [0.5, 0.5],
children: [
{ kind: "pane", pane: { id: "left", tabIds: ["a"], focusedTabId: "a" } },
{ kind: "pane", pane: { id: "right", tabIds: ["b"], focusedTabId: "b" } },
],
},
},
};
const tabs: WorkspaceTab[] = [
{ tabId: "a", target: { kind: "agent", agentId: "agent-a" }, createdAt: 1 },
{ tabId: "b", target: { kind: "agent", agentId: "agent-b" }, createdAt: 2 },
];
expect(
selectVisibleAgentIds({ layout, tabs, routeFocused: true, focusedPaneOnly: true }),
).toEqual(["agent-b"]);
});
test("pane retargeting replaces the viewed agent and duplicate panes collapse to one ID", () => {
const layout: WorkspaceLayout = {
focusedPaneId: "left",
root: {
kind: "group",
group: {
id: "root",
direction: "horizontal",
sizes: [0.5, 0.5],
children: [
{ kind: "pane", pane: { id: "left", tabIds: ["active"], focusedTabId: "active" } },
{ kind: "pane", pane: { id: "right", tabIds: ["duplicate"], focusedTabId: "duplicate" } },
],
},
},
};
const duplicateTabs: WorkspaceTab[] = [
{ tabId: "active", target: { kind: "agent", agentId: "agent-a" }, createdAt: 1 },
{ tabId: "duplicate", target: { kind: "agent", agentId: "agent-a" }, createdAt: 2 },
];
const retargetedTabs: WorkspaceTab[] = [
{ tabId: "active", target: { kind: "agent", agentId: "agent-b" }, createdAt: 1 },
{ tabId: "duplicate", target: { kind: "agent", agentId: "agent-a" }, createdAt: 2 },
];
expect({
duplicate: selectVisibleAgentIds({
layout,
tabs: duplicateTabs,
routeFocused: true,
focusedPaneOnly: false,
}),
retargeted: selectVisibleAgentIds({
layout,
tabs: retargetedTabs,
routeFocused: true,
focusedPaneOnly: false,
}),
}).toEqual({ duplicate: ["agent-a"], retargeted: ["agent-a", "agent-b"] });
});
@@ -0,0 +1,27 @@
import { collectAllPanes, type WorkspaceLayout } from "@/stores/workspace-layout-store";
import type { WorkspaceTab } from "@/stores/workspace-tabs-store";
import { deriveWorkspacePaneState } from "./workspace-pane-state";
export function selectVisibleAgentIds(input: {
layout: WorkspaceLayout | null;
tabs: WorkspaceTab[];
routeFocused: boolean;
focusedPaneOnly: boolean;
}): string[] {
if (!input.routeFocused || !input.layout) {
return [];
}
const panes = input.focusedPaneOnly
? collectAllPanes(input.layout.root).filter((pane) => pane.id === input.layout?.focusedPaneId)
: collectAllPanes(input.layout.root);
return [
...new Set(
panes.flatMap((pane) => {
const target = deriveWorkspacePaneState({ pane, tabs: input.tabs }).activeTab?.descriptor
.target;
return target?.kind === "agent" ? [target.agentId] : [];
}),
),
].sort();
}
@@ -89,6 +89,7 @@ import {
normalizeWorkspaceTabTarget,
workspaceTabTargetsEqual,
} from "@/workspace-tabs/identity";
import { selectVisibleAgentIds } from "./visible-agent-ids";
import {
getHostRuntimeStore,
useHostRuntimeClient,
@@ -2013,6 +2014,31 @@ function WorkspaceScreenContent({
}),
[uiTabs, workspaceLayout],
);
const viewedTimelineSync = useSessionStore(
(state) => state.sessions[normalizedServerId]?.viewedTimelineSync ?? null,
);
const visibleAgentIds = useMemo(
() =>
selectVisibleAgentIds({
layout: workspaceLayout,
tabs: uiTabs,
routeFocused: isRouteFocused,
focusedPaneOnly: isMobile || isFocusModeEnabled || !supportsDesktopPaneSplits(),
}),
[isFocusModeEnabled, isMobile, isRouteFocused, uiTabs, workspaceLayout],
);
useEffect(() => {
if (!persistenceKey || !viewedTimelineSync) {
return;
}
viewedTimelineSync.replaceVisibleAgentIds(persistenceKey, visibleAgentIds);
}, [persistenceKey, viewedTimelineSync, visibleAgentIds]);
useEffect(() => {
if (!persistenceKey || !viewedTimelineSync) {
return;
}
return () => viewedTimelineSync.replaceVisibleAgentIds(persistenceKey, []);
}, [persistenceKey, viewedTimelineSync]);
const setFocusedAgentId = useSessionStore((state) => state.setFocusedAgentId);
const setFocusedTerminalId = useSessionStore((state) => state.setFocusedTerminalId);
const focusedPaneAgentId = useMemo(() => {
@@ -2740,7 +2766,7 @@ function WorkspaceScreenContent({
// dropped against the stale cursor.
const sessionState = useSessionStore.getState().sessions[normalizedServerId];
const currentCursor = sessionState?.agentTimelineCursor.get(agentId);
await client.fetchAgentTimeline(agentId, {
await getHostRuntimeStore().fetchAgentTimeline(normalizedServerId, agentId, {
direction: "tail",
projection: "projected",
...(currentCursor