diff --git a/workspace/frontend/lib/sidebar-agents.ts b/workspace/frontend/lib/sidebar-agents.ts index 0941f3d1d..2728c2c14 100644 --- a/workspace/frontend/lib/sidebar-agents.ts +++ b/workspace/frontend/lib/sidebar-agents.ts @@ -3,5 +3,20 @@ import type { WorkspaceAgent } from './types'; /** The personal roster never includes other people's or unclaimed agents. */ export function ownedSidebarAgents(agents: WorkspaceAgent[], email: string) { const owner = email.trim().toLowerCase(); - return owner ? agents.filter((agent) => agent.ownerEmail?.trim().toLowerCase() === owner && agent.status !== 'removed') : []; + return owner + ? agents + .filter((agent) => agent.ownerEmail?.trim().toLowerCase() === owner && agent.status !== 'removed') + .sort((a, b) => { + const aJoinedAt = a.joinedAt ? Date.parse(a.joinedAt) : Number.NaN; + const bJoinedAt = b.joinedAt ? Date.parse(b.joinedAt) : Number.NaN; + const aHasJoinedAt = Number.isFinite(aJoinedAt); + const bHasJoinedAt = Number.isFinite(bJoinedAt); + + if (aHasJoinedAt && bHasJoinedAt && aJoinedAt !== bJoinedAt) { + return aJoinedAt - bJoinedAt; + } + if (aHasJoinedAt !== bHasJoinedAt) return aHasJoinedAt ? -1 : 1; + return a.agentName.localeCompare(b.agentName); + }) + : []; }