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:
@@ -2,8 +2,132 @@ import type { FetchAgentsEntry } from "@getpaseo/client/internal/daemon-client";
|
||||
import { type Agent, useSessionStore } from "@/stores/session-store";
|
||||
import { derivePendingPermissionKey, normalizeAgentSnapshot } from "@/utils/agent-snapshots";
|
||||
import { resolveProjectPlacement } from "@/utils/project-placement";
|
||||
import type { SessionOutboundMessage } from "@getpaseo/protocol/messages";
|
||||
import { clearArchiveAgentPending } from "@/hooks/use-archive-agent";
|
||||
import { queryClient } from "@/data/query-client";
|
||||
import { acceptAgentDirectoryUpdate } from "@/utils/agent-directory-update-policy";
|
||||
import { buildDraftStoreKey } from "@/stores/draft-keys";
|
||||
import { useDraftStore } from "@/stores/draft-store";
|
||||
import { getInitDeferred, getInitKey, rejectInitDeferred } from "@/utils/agent-initialization";
|
||||
|
||||
type AgentDirectoryFetchEntry = FetchAgentsEntry;
|
||||
export type AgentDirectoryDelta = Extract<
|
||||
SessionOutboundMessage,
|
||||
{ type: "agent_update" }
|
||||
>["payload"];
|
||||
|
||||
export function applyAgentDirectoryDelta(input: { serverId: string; delta: AgentDirectoryDelta }): {
|
||||
agentId: string;
|
||||
stoppedRunning: boolean;
|
||||
} {
|
||||
if (input.delta.kind === "remove") {
|
||||
removeAgentDirectoryReplica(input.serverId, input.delta.agentId);
|
||||
return { agentId: input.delta.agentId, stoppedRunning: false };
|
||||
}
|
||||
return upsertAgentDirectoryReplica(input.serverId, input.delta);
|
||||
}
|
||||
|
||||
type AgentUpsertDelta = Extract<AgentDirectoryDelta, { kind: "upsert" }>;
|
||||
|
||||
function upsertAgentDirectoryReplica(
|
||||
serverId: string,
|
||||
delta: AgentUpsertDelta,
|
||||
): { agentId: string; stoppedRunning: boolean } {
|
||||
const normalized = normalizeAgentSnapshot(delta.agent, serverId);
|
||||
const session = useSessionStore.getState().sessions[serverId];
|
||||
const previousAgent =
|
||||
session?.agents.get(normalized.id) ?? session?.agentDetails.get(normalized.id);
|
||||
const legacyWorkspaceId =
|
||||
previousAgent?.workspaceId ??
|
||||
Array.from(session?.workspaces.values() ?? []).find(
|
||||
(workspace) =>
|
||||
session?.serverInfo?.features?.workspaceMultiplicity !== true &&
|
||||
workspace.workspaceDirectory === normalized.cwd,
|
||||
)?.id;
|
||||
const agent: Agent = {
|
||||
...normalized,
|
||||
workspaceId: normalized.workspaceId ?? legacyWorkspaceId,
|
||||
projectPlacement:
|
||||
resolveProjectPlacement({ projectPlacement: delta.project, cwd: normalized.cwd }) ??
|
||||
previousAgent?.projectPlacement,
|
||||
};
|
||||
const acceptedAgent = upsertAgentReplica(serverId, agent);
|
||||
if (acceptedAgent.archivedAt) {
|
||||
clearArchiveAgentPending({ queryClient, serverId, agentId: acceptedAgent.id });
|
||||
}
|
||||
replaceAgentPendingPermissions(serverId, acceptedAgent);
|
||||
useSessionStore.getState().setAgentLastActivity(acceptedAgent.id, acceptedAgent.lastActivityAt);
|
||||
return {
|
||||
agentId: acceptedAgent.id,
|
||||
stoppedRunning: previousAgent?.status === "running" && acceptedAgent.status !== "running",
|
||||
};
|
||||
}
|
||||
|
||||
export function upsertAgentReplica(serverId: string, agent: Agent): Agent {
|
||||
let acceptedAgent = agent;
|
||||
useSessionStore.getState().setAgents(serverId, (current) => {
|
||||
const currentAgent = current.get(agent.id);
|
||||
acceptedAgent = acceptAgentDirectoryUpdate(currentAgent, agent);
|
||||
if (acceptedAgent === currentAgent) return current;
|
||||
const next = new Map(current);
|
||||
next.set(agent.id, acceptedAgent);
|
||||
return next;
|
||||
});
|
||||
return acceptedAgent;
|
||||
}
|
||||
|
||||
export function replaceAgentPendingPermissions(serverId: string, agent: Agent): void {
|
||||
const pendingPermissions = new Map(
|
||||
useSessionStore.getState().sessions[serverId]?.pendingPermissions,
|
||||
);
|
||||
for (const [key, pending] of pendingPermissions) {
|
||||
if (pending.agentId === agent.id) pendingPermissions.delete(key);
|
||||
}
|
||||
for (const request of agent.pendingPermissions) {
|
||||
const key = derivePendingPermissionKey(agent.id, request);
|
||||
pendingPermissions.set(key, { key, agentId: agent.id, request });
|
||||
}
|
||||
useSessionStore.getState().setPendingPermissions(serverId, pendingPermissions);
|
||||
}
|
||||
|
||||
export function removeAgentDirectoryReplica(serverId: string, agentId: string): void {
|
||||
const store = useSessionStore.getState();
|
||||
clearArchiveAgentPending({ queryClient, serverId, agentId });
|
||||
const removeKey = <T>(current: Map<string, T>): Map<string, T> => {
|
||||
if (!current.has(agentId)) return current;
|
||||
const next = new Map(current);
|
||||
next.delete(agentId);
|
||||
return next;
|
||||
};
|
||||
store.setAgents(serverId, removeKey);
|
||||
store.setAgentDetails(serverId, removeKey);
|
||||
store.setQueuedMessages(serverId, removeKey);
|
||||
store.setAgentTimelineCursor(serverId, removeKey);
|
||||
store.setInitializingAgents(serverId, removeKey);
|
||||
store.setPendingPermissions(serverId, (current) => {
|
||||
const next = new Map(current);
|
||||
for (const [key, pending] of next) {
|
||||
if (pending.agentId === agentId) next.delete(key);
|
||||
}
|
||||
return next.size === current.size ? current : next;
|
||||
});
|
||||
store.setAgentAuthoritativeHistoryApplied(serverId, agentId, false);
|
||||
store.setAgentStreamTail(serverId, removeKey);
|
||||
store.clearAgentStreamHead(serverId, agentId);
|
||||
useSessionStore.setState((state) => {
|
||||
if (!state.agentLastActivity.has(agentId)) return state;
|
||||
const agentLastActivity = new Map(state.agentLastActivity);
|
||||
agentLastActivity.delete(agentId);
|
||||
return { ...state, agentLastActivity };
|
||||
});
|
||||
useDraftStore.getState().clearDraftInput({
|
||||
draftKey: buildDraftStoreKey({ serverId, agentId }),
|
||||
});
|
||||
const initKey = getInitKey(serverId, agentId);
|
||||
if (getInitDeferred(initKey)) {
|
||||
rejectInitDeferred(initKey, new Error("Agent was removed during initialization"));
|
||||
}
|
||||
}
|
||||
|
||||
interface PendingPermissionEntry {
|
||||
key: string;
|
||||
@@ -49,6 +173,12 @@ export function replaceFetchedAgentDirectory(input: {
|
||||
const { agents: fetchedAgents, pendingPermissions } = buildAgentDirectoryState(input);
|
||||
const store = useSessionStore.getState();
|
||||
|
||||
for (const agent of fetchedAgents.values()) {
|
||||
if (agent.archivedAt) {
|
||||
clearArchiveAgentPending({ queryClient, serverId: input.serverId, agentId: agent.id });
|
||||
}
|
||||
}
|
||||
|
||||
store.setAgents(input.serverId, fetchedAgents);
|
||||
store.setAgentDetails(input.serverId, (prev) => {
|
||||
let next: Map<string, Agent> | null = null;
|
||||
@@ -69,7 +199,6 @@ export function replaceFetchedAgentDirectory(input: {
|
||||
store.setAgentLastActivityBatch(lastActivityByAgentId);
|
||||
|
||||
store.setPendingPermissions(input.serverId, new Map(pendingPermissions));
|
||||
store.setInitializingAgents(input.serverId, new Map());
|
||||
store.setHasHydratedAgents(input.serverId, true);
|
||||
return { agents: fetchedAgents };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user