Skip to content

Commit fb74776

Browse files
committed
perf(sessions): case-variant fallback scans keys, not parsed entries
Review caught that a per-row miss still parsed the full store. The fallback only needs persisted keys to find a legacy case-variant, so list keys without touching entry_json and probe the single winner.
1 parent 4082dc1 commit fb74776

5 files changed

Lines changed: 34 additions & 29 deletions

File tree

‎src/acp/runtime/session-meta-store.ts‎

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import { resolveDefaultAgentId } from "../../agents/agent-scope-config.js";
44
import { getRuntimeConfig } from "../../config/config.js";
55
import { resolveStorePath } from "../../config/sessions/paths.js";
66
import {
7-
listSessionEntriesReadOnly,
7+
listSessionEntryKeysReadOnly,
88
loadExactSessionEntryReadOnly,
9-
type SessionEntrySummary,
109
} from "../../config/sessions/session-accessor.js";
1110
import type { SessionEntry } from "../../config/sessions/types.js";
1211
import type { OpenClawConfig } from "../../config/types.openclaw.js";
@@ -45,37 +44,20 @@ export function resolveStoreEntryForSessionKey(params: {
4544
return { storeSessionKey: lower, entry: lowered.entry };
4645
}
4746
}
48-
const entries = listSessionEntriesReadOnly(scope);
49-
const storeSessionKey = resolveStoreSessionKey(entries, normalized);
47+
// Both direct probes missed, so only a legacy case-variant key can match.
48+
// Scan persisted keys without parsing any entry_json, then probe the winner.
49+
const variant = listSessionEntryKeysReadOnly(scope).find(
50+
(candidate) => normalizeLowercaseStringOrEmpty(candidate) === lower,
51+
);
52+
if (variant === undefined) {
53+
return { storeSessionKey: lower };
54+
}
5055
return {
51-
storeSessionKey,
52-
entry: entries.find((candidate) => candidate.sessionKey === storeSessionKey)?.entry,
56+
storeSessionKey: variant,
57+
entry: loadExactSessionEntryReadOnly({ ...scope, sessionKey: variant })?.entry,
5358
};
5459
}
5560

56-
function resolveStoreSessionKey(
57-
entries: readonly SessionEntrySummary[],
58-
sessionKey: string,
59-
): string {
60-
const normalized = sessionKey.trim();
61-
if (!normalized) {
62-
return "";
63-
}
64-
if (entries.some((entry) => entry.sessionKey === normalized)) {
65-
return normalized;
66-
}
67-
const lower = normalizeLowercaseStringOrEmpty(normalized);
68-
if (entries.some((entry) => entry.sessionKey === lower)) {
69-
return lower;
70-
}
71-
for (const entry of entries) {
72-
if (normalizeLowercaseStringOrEmpty(entry.sessionKey) === lower) {
73-
return entry.sessionKey;
74-
}
75-
}
76-
return lower;
77-
}
78-
7961
/** Resolves the session store path that owns an ACP session key. */
8062
export function resolveSessionStorePathForAcp(params: {
8163
sessionKey: string;

‎src/config/sessions/session-accessor.entry.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { clearPluginOwnedSessionState } from "./plugin-host-cleanup.js";
1212
import {
1313
listSqliteSessionEntries,
1414
listSqliteSessionEntriesReadOnly,
15+
listSqliteSessionEntryKeysReadOnly,
1516
loadExactSqliteSessionEntry,
1617
loadExactSqliteSessionEntryReadOnly,
1718
loadSqliteSessionEntry,
@@ -356,6 +357,11 @@ export function listSessionEntriesReadOnly(
356357
return listSqliteSessionEntriesReadOnly(scope);
357358
}
358359

360+
/** Lists persisted session keys only; the fallback probe for case-variant keys. */
361+
export function listSessionEntryKeysReadOnly(scope: SessionEntryListScope = {}): string[] {
362+
return listSqliteSessionEntryKeysReadOnly(scope);
363+
}
364+
359365
/**
360366
* Borrowed keyed view over one resolved store for synchronous read-only hot paths.
361367
* Unlike loadSessionEntry, `get` is a raw exact persisted-key probe with no alias

‎src/config/sessions/session-accessor.sqlite-entry.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ export function loadExactSqliteSessionEntry(
110110
return row ? { sessionKey, entry: row.entry } : undefined;
111111
}
112112

113+
/** Lists persisted session keys only, skipping entry_json parsing entirely. */
114+
export function listSqliteSessionEntryKeysReadOnly(
115+
scope: Partial<Omit<SessionAccessScope, "sessionKey">> = {},
116+
): string[] {
117+
const resolved = resolveSqliteScope({ ...scope, sessionKey: "" });
118+
const result = withOpenClawAgentDatabaseReadOnly((database) => {
119+
const db = getSessionKysely(database.db);
120+
return executeSqliteQuerySync(
121+
database.db,
122+
db.selectFrom("session_nodes").select(["session_key"]).orderBy("session_key", "asc"),
123+
).rows.map((row) => row.session_key);
124+
}, toDatabaseOptions(resolved));
125+
return result.found ? result.value : [];
126+
}
127+
113128
/** Exact persisted-key probe on the read-only handle, for per-row hot paths. */
114129
export function loadExactSqliteSessionEntryReadOnly(
115130
scope: SessionAccessScope,

‎src/config/sessions/session-accessor.sqlite.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export {
33
listSqliteSessionEntries,
44
listSqliteSessionEntriesReadOnly,
5+
listSqliteSessionEntryKeysReadOnly,
56
listSqliteSessionEntriesByStatus,
67
listSqliteSessionTranscriptInstances,
78
loadExactSqliteSessionEntry,

‎src/config/sessions/session-accessor.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export {
123123
clearPluginOwnedSessionState,
124124
listSessionEntries,
125125
listSessionEntriesReadOnly,
126+
listSessionEntryKeysReadOnly,
126127
loadExactSessionEntry,
127128
loadExactSessionEntryReadOnly,
128129
loadSessionEntry,

0 commit comments

Comments
 (0)