Skip to content

Commit 962a6bf

Browse files
ly-wang19vincentkoc
authored andcommitted
perf(status): select recent sessions without full sort
1 parent eba1ca6 commit 962a6bf

2 files changed

Lines changed: 63 additions & 7 deletions

File tree

src/commands/status.summary.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,40 @@ describe("getStatusSummary", () => {
510510
expect(hydratedKeys).not.toContain("agent:main:session-2");
511511
});
512512

513+
it("preserves store order for tied recent session timestamps", async () => {
514+
const store = Object.fromEntries(
515+
Array.from({ length: 11 }, (_, index) => {
516+
const number = index + 1;
517+
return [
518+
`agent:main:session-${number}`,
519+
{
520+
sessionId: `session-${number}`,
521+
updatedAt: 1,
522+
},
523+
];
524+
}),
525+
);
526+
statusSummaryMocks.listSessionEntries.mockReturnValue(toSessionEntrySummaries(store));
527+
528+
const summary = await getStatusSummary();
529+
530+
expect(summary.sessions.recent.map((session) => session.key)).toEqual([
531+
"agent:main:session-1",
532+
"agent:main:session-2",
533+
"agent:main:session-3",
534+
"agent:main:session-4",
535+
"agent:main:session-5",
536+
"agent:main:session-6",
537+
"agent:main:session-7",
538+
"agent:main:session-8",
539+
"agent:main:session-9",
540+
"agent:main:session-10",
541+
]);
542+
expect(summary.sessions.byAgent[0]?.recent.map((session) => session.key)).toEqual(
543+
summary.sessions.recent.map((session) => session.key),
544+
);
545+
});
546+
513547
it("passes agent scope when listing configured agent session stores", async () => {
514548
vi.mocked(listGatewayAgentsBasic).mockReturnValue({
515549
defaultId: "main",

src/commands/status.summary.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,27 @@ function compareSessionCandidatesByUpdatedAt(left: SessionCandidate, right: Sess
180180
return (right.updatedAt ?? 0) - (left.updatedAt ?? 0);
181181
}
182182

183+
function selectRecentSessionCandidates(
184+
candidates: SessionCandidate[],
185+
limit: number,
186+
): SessionCandidate[] {
187+
const selected: SessionCandidate[] = [];
188+
for (const candidate of candidates) {
189+
const insertAt = selected.findIndex(
190+
(selectedCandidate) => compareSessionCandidatesByUpdatedAt(candidate, selectedCandidate) < 0,
191+
);
192+
if (insertAt >= 0) {
193+
selected.splice(insertAt, 0, candidate);
194+
if (selected.length > limit) {
195+
selected.pop();
196+
}
197+
} else if (selected.length < limit) {
198+
selected.push(candidate);
199+
}
200+
}
201+
return selected;
202+
}
203+
183204
function listSessionCandidates(storePath: string, agentId?: string) {
184205
return (
185206
listSessionEntries({
@@ -193,7 +214,6 @@ function listSessionCandidates(storePath: string, agentId?: string) {
193214
entry,
194215
updatedAt: entry?.updatedAt ?? null,
195216
}))
196-
.toSorted(compareSessionCandidatesByUpdatedAt)
197217
);
198218
}
199219

@@ -471,9 +491,10 @@ export async function getStatusSummary(
471491
agentList.agents.map(async (agent) => {
472492
const storePath = resolveStorePath(cfg.session?.store, { agentId: agent.id });
473493
const candidates = loadSessionCandidates(storePath, agent.id);
474-
const sessions = await buildSessionRows(candidates.slice(0, RECENT_SESSION_LIMIT), {
475-
agentIdOverride: agent.id,
476-
});
494+
const sessions = await buildSessionRows(
495+
selectRecentSessionCandidates(candidates, RECENT_SESSION_LIMIT),
496+
{ agentIdOverride: agent.id },
497+
);
477498
return {
478499
agentId: agent.id,
479500
path: storePath,
@@ -492,9 +513,10 @@ export async function getStatusSummary(
492513
source.storePath,
493514
pathCounts.get(source.storePath) === 1 ? source.agentId : undefined,
494515
),
495-
)
496-
.toSorted((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0));
497-
const recent = await buildSessionRows(allSessions.slice(0, RECENT_SESSION_LIMIT));
516+
);
517+
const recent = await buildSessionRows(
518+
selectRecentSessionCandidates(allSessions, RECENT_SESSION_LIMIT),
519+
);
498520
const totalSessions = allSessions.length;
499521

500522
const summary: StatusSummary = {

0 commit comments

Comments
 (0)