perf: avoid sorting session lookup paths

This commit is contained in:
Shakker
2026-05-08 02:40:01 +01:00
parent 7d4011862a
commit d2cd9badd9
4 changed files with 30 additions and 8 deletions

View File

@@ -15,6 +15,7 @@ Docs: https://docs.openclaw.ai
- Runtime/performance: avoid full-array sorting while auto-selecting providers, resolving supported thinking levels, picking node last-seen timestamps, and extracting Codex usage-limit messages. Thanks @shakkernerd.
- Plugins/doctor: avoid full-array sorting while selecting ClawHub search/archive results and bounded dreaming doctor entries. Thanks @shakkernerd.
- Agents/compaction: keep contributor diagnostics to a bounded top-three selection without sorting the full history. Thanks @shakkernerd.
- Sessions/UI: avoid full-array sorting while selecting ACPX leases, Google Meet calendar events, and latest chat sessions. Thanks @shakkernerd.
- Telegram: preserve the channel-specific 10-option poll cap in the unified outbound adapter so over-limit polls are rejected before send. (#78762) Thanks @obviyus.
- Slack: route handled top-level channel turns in implicit-conversation channels to thread-scoped sessions when Slack reply threading is enabled, keeping the root turn and later thread replies on one OpenClaw session. (#78522) Thanks @zeroth-blip.
- Telegram: re-probe the primary fetch transport after repeated sticky fallback success so transient IPv4 or pinned-IP fallback promotion can recover without a gateway restart. Fixes #77088. (#77157) Thanks @MkDev11.

View File

@@ -133,7 +133,13 @@ function selectCurrentSessionLease(params: {
if (params.rootPid) {
return candidates.find((lease) => lease.rootPid === params.rootPid);
}
return candidates.toSorted((a, b) => b.startedAt - a.startedAt)[0];
let selected: AcpxProcessLease | undefined;
for (const lease of candidates) {
if (!selected || lease.startedAt > selected.startedAt) {
selected = lease;
}
}
return selected;
}
function createResetAwareSessionStore(

View File

@@ -138,10 +138,19 @@ function chooseBestMeetCalendarEvent(
now: Date,
): GoogleMeetCalendarLookupResult["event"] | undefined {
const nowMs = now.getTime();
return events
.filter((event) => event.status !== "cancelled")
.filter((event) => extractGoogleMeetUriFromCalendarEvent(event))
.toSorted((left, right) => rankCalendarEvent(left, nowMs) - rankCalendarEvent(right, nowMs))[0];
let selected: GoogleMeetCalendarEvent | undefined;
let selectedRank = Number.POSITIVE_INFINITY;
for (const event of events) {
if (event.status === "cancelled" || !extractGoogleMeetUriFromCalendarEvent(event)) {
continue;
}
const rank = rankCalendarEvent(event, nowMs);
if (!selected || rank < selectedRank) {
selected = event;
selectedRank = rank;
}
}
return selected;
}
async function fetchGoogleCalendarEvents(params: {

View File

@@ -593,9 +593,15 @@ function resolvePreferredSessionForAgent(state: AppViewState, agentId: string):
return state.sessionKey;
}
const rows = state.sessionsResult?.sessions ?? [];
const row = rows
.filter((entry) => isSessionKeyTiedToAgent(entry.key, normalizedAgentId, defaultAgentId))
.toSorted((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0))[0];
let row: (typeof rows)[number] | undefined;
for (const entry of rows) {
if (!isSessionKeyTiedToAgent(entry.key, normalizedAgentId, defaultAgentId)) {
continue;
}
if (!row || (entry.updatedAt ?? 0) > (row.updatedAt ?? 0)) {
row = entry;
}
}
return row?.key ?? buildAgentMainSessionKey({ agentId: normalizedAgentId });
}