refactor: trim qqbot session store probes

This commit is contained in:
Peter Steinberger
2026-05-02 09:29:18 +01:00
parent b65946b044
commit befd4124f7
2 changed files with 1 additions and 75 deletions

View File

@@ -62,16 +62,6 @@ function getCandidateSessionPaths(accountId: string): string[] {
return primaryPath === legacyPath ? [primaryPath] : [primaryPath, legacyPath];
}
function isSessionFileName(file: string): boolean {
return file.startsWith("session-") && file.endsWith(".json");
}
function readSessionStateFile(file: string): { filePath: string; state: SessionState } {
const filePath = path.join(getSessionDir(), file);
const data = fs.readFileSync(filePath, "utf-8");
return { filePath, state: JSON.parse(data) as SessionState };
}
/** Load a saved session, rejecting expired or mismatched appId entries. */
export function loadSession(accountId: string, expectedAppId?: string): SessionState | null {
try {
@@ -212,65 +202,3 @@ export function clearSession(accountId: string): void {
);
}
}
/** Load all saved sessions from disk. */
export function getAllSessions(): SessionState[] {
const sessions = new Map<string, SessionState>();
try {
const sessionDir = getSessionDir();
if (!fs.existsSync(sessionDir)) {
return [];
}
const files = fs.readdirSync(sessionDir);
for (const file of files) {
if (isSessionFileName(file)) {
try {
const { state } = readSessionStateFile(file);
if (typeof state.accountId !== "string" || !state.accountId) {
continue;
}
const existing = sessions.get(state.accountId);
if (!existing || (state.savedAt ?? 0) >= (existing.savedAt ?? 0)) {
sessions.set(state.accountId, state);
}
} catch {}
}
}
} catch {}
return [...sessions.values()];
}
/** Remove expired session files from disk. */
export function cleanupExpiredSessions(): number {
let cleaned = 0;
try {
const sessionDir = getSessionDir();
if (!fs.existsSync(sessionDir)) {
return 0;
}
const now = Date.now();
const files = fs.readdirSync(sessionDir);
for (const file of files) {
if (isSessionFileName(file)) {
const filePath = path.join(sessionDir, file);
try {
const { state } = readSessionStateFile(file);
if (now - state.savedAt > SESSION_EXPIRE_TIME) {
fs.unlinkSync(filePath);
cleaned++;
debugLog(`[session-store] Cleaned expired session: ${file}`);
}
} catch {
try {
fs.unlinkSync(filePath);
cleaned++;
} catch {}
}
}
}
} catch {}
return cleaned;
}

View File

@@ -38,14 +38,12 @@ describe("qqbot storage laziness", () => {
const qqbotRoot = path.join(homeDir, ".openclaw", "qqbot");
const sessionStore = await import("../session/session-store.js");
await import("../session/session-store.js");
await import("../session/known-users.js");
await import("../ref/store.js");
const { loadCredentialBackup } = await import("../config/credential-backup.js");
expect(loadCredentialBackup("default")).toBeNull();
expect(sessionStore.getAllSessions()).toEqual([]);
expect(sessionStore.cleanupExpiredSessions()).toBe(0);
expect(fs.existsSync(qqbotRoot)).toBe(false);
});