diff --git a/src/plugin/hooks/create-session-hooks.ts b/src/plugin/hooks/create-session-hooks.ts index 82a4379f..d93ec585 100644 --- a/src/plugin/hooks/create-session-hooks.ts +++ b/src/plugin/hooks/create-session-hooks.ts @@ -122,7 +122,7 @@ export function createSessionHooks(args: { ? safeHook("ralph-loop", () => createRalphLoopHook(ctx, { config: pluginConfig.ralph_loop, - checkSessionExists: async (sessionId) => sessionExists(sessionId), + checkSessionExists: async (sessionId) => await sessionExists(sessionId), })) : null diff --git a/src/tools/session-manager/storage.test.ts b/src/tools/session-manager/storage.test.ts index abeeb951..81e3048e 100644 --- a/src/tools/session-manager/storage.test.ts +++ b/src/tools/session-manager/storage.test.ts @@ -78,15 +78,15 @@ describe("session-manager storage", () => { expect(result).toBe(sessionPath) }) - test("sessionExists returns false for non-existent session", () => { + test("sessionExists returns false for non-existent session", async () => { // when - const exists = sessionExists("ses_nonexistent") + const exists = await sessionExists("ses_nonexistent") // then expect(exists).toBe(false) }) - test("sessionExists returns true for existing session", () => { + test("sessionExists returns true for existing session", async () => { // given const sessionID = "ses_exists" const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID) @@ -94,7 +94,7 @@ describe("session-manager storage", () => { writeFileSync(join(sessionPath, "msg_001.json"), JSON.stringify({ id: "msg_001" })) // when - const exists = sessionExists(sessionID) + const exists = await sessionExists(sessionID) // then expect(exists).toBe(true) diff --git a/src/tools/session-manager/storage.ts b/src/tools/session-manager/storage.ts index 8e4f4393..530782af 100644 --- a/src/tools/session-manager/storage.ts +++ b/src/tools/session-manager/storage.ts @@ -138,8 +138,16 @@ export function getMessageDir(sessionID: string): string | null { return null } -export function sessionExists(sessionID: string): boolean { - if (isSqliteBackend()) return true +export async function sessionExists(sessionID: string): Promise { + if (isSqliteBackend() && sdkClient) { + try { + const response = await sdkClient.session.messages({ path: { id: sessionID } }) + const messages = response.data as unknown[] | undefined + return Array.isArray(messages) && messages.length > 0 + } catch { + return false + } + } return getMessageDir(sessionID) !== null } diff --git a/src/tools/session-manager/tools.ts b/src/tools/session-manager/tools.ts index 35d58a79..e620c55b 100644 --- a/src/tools/session-manager/tools.ts +++ b/src/tools/session-manager/tools.ts @@ -70,7 +70,7 @@ export function createSessionManagerTools(ctx: PluginInput): Record { try { - if (!sessionExists(args.session_id)) { + if (!(await sessionExists(args.session_id))) { return `Session not found: ${args.session_id}` }