perf: bound ACP metadata key repair lookup

This commit is contained in:
Shakker
2026-06-09 19:38:14 +01:00
committed by Shakker
parent 21104cd52e
commit 3b7631e50d
2 changed files with 25 additions and 2 deletions
+24 -2
View File
@@ -253,6 +253,7 @@ export function writeAcpSessionMetaForMigration(params: {
export function repairAcpSessionMetaKeyForMigration(params: {
sessionKey: string;
candidateSessionKeys?: Iterable<string | null | undefined>;
entry?: Pick<SessionEntry, "sessionId">;
env?: NodeJS.ProcessEnv;
databasePath?: string;
@@ -272,17 +273,38 @@ export function repairAcpSessionMetaKeyForMigration(params: {
}
const normalizedSessionKey = normalizeLowercaseStringOrEmpty(sessionKey);
const row = executeSqliteQuerySync(
const candidateKeys = new Set<string>();
candidateKeys.add(normalizedSessionKey);
for (const candidate of params.candidateSessionKeys ?? []) {
const trimmed = typeof candidate === "string" ? candidate.trim() : "";
if (
trimmed &&
trimmed !== sessionKey &&
normalizeLowercaseStringOrEmpty(trimmed) === normalizedSessionKey
) {
candidateKeys.add(trimmed);
}
}
let row: AcpSessionRow | undefined;
for (const candidateKey of candidateKeys) {
const candidateRow = selectAcpSessionRow(database.db, candidateKey);
if (candidateRow && acpSessionRowMatchesEntry(candidateRow, params.entry)) {
row = candidateRow;
break;
}
}
row ??= executeSqliteQuerySync(
database.db,
getAcpSessionKysely(database.db)
.selectFrom("acp_sessions")
.selectAll()
.where((eb) => eb.fn<string>("lower", ["session_key"]), "=", normalizedSessionKey)
.orderBy("last_activity_at", "desc")
.orderBy("session_key", "asc"),
).rows.find(
(candidate) =>
candidate.session_key !== sessionKey &&
normalizeLowercaseStringOrEmpty(candidate.session_key) === normalizedSessionKey &&
acpSessionRowMatchesEntry(candidate, params.entry),
);
if (!row) {
+1
View File
@@ -951,6 +951,7 @@ function readAcpMetaForDeletedAgentCheck(params: {
repairAcpSessionMetaKeyForMigration({
sessionKey: params.sessionKey,
candidateSessionKeys: directKeys,
entry: params.entry ?? undefined,
});
return readAcpSessionMetaForEntry({