fix: make sessionExists() async with SDK verification on SQLite
sessionExists() previously returned unconditional true on SQLite, preventing ralph-loop orphaned-session cleanup from triggering. Now uses sdkClient.session.messages() to verify session actually exists. Callers updated to await the async result. Addresses Cubic review feedback on PR #1837.
This commit is contained in:
parent
3bbe0cbb1d
commit
11586445cf
@ -122,7 +122,7 @@ export function createSessionHooks(args: {
|
|||||||
? safeHook("ralph-loop", () =>
|
? safeHook("ralph-loop", () =>
|
||||||
createRalphLoopHook(ctx, {
|
createRalphLoopHook(ctx, {
|
||||||
config: pluginConfig.ralph_loop,
|
config: pluginConfig.ralph_loop,
|
||||||
checkSessionExists: async (sessionId) => sessionExists(sessionId),
|
checkSessionExists: async (sessionId) => await sessionExists(sessionId),
|
||||||
}))
|
}))
|
||||||
: null
|
: null
|
||||||
|
|
||||||
|
|||||||
@ -78,15 +78,15 @@ describe("session-manager storage", () => {
|
|||||||
expect(result).toBe(sessionPath)
|
expect(result).toBe(sessionPath)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("sessionExists returns false for non-existent session", () => {
|
test("sessionExists returns false for non-existent session", async () => {
|
||||||
// when
|
// when
|
||||||
const exists = sessionExists("ses_nonexistent")
|
const exists = await sessionExists("ses_nonexistent")
|
||||||
|
|
||||||
// then
|
// then
|
||||||
expect(exists).toBe(false)
|
expect(exists).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("sessionExists returns true for existing session", () => {
|
test("sessionExists returns true for existing session", async () => {
|
||||||
// given
|
// given
|
||||||
const sessionID = "ses_exists"
|
const sessionID = "ses_exists"
|
||||||
const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID)
|
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" }))
|
writeFileSync(join(sessionPath, "msg_001.json"), JSON.stringify({ id: "msg_001" }))
|
||||||
|
|
||||||
// when
|
// when
|
||||||
const exists = sessionExists(sessionID)
|
const exists = await sessionExists(sessionID)
|
||||||
|
|
||||||
// then
|
// then
|
||||||
expect(exists).toBe(true)
|
expect(exists).toBe(true)
|
||||||
|
|||||||
@ -138,8 +138,16 @@ export function getMessageDir(sessionID: string): string | null {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sessionExists(sessionID: string): boolean {
|
export async function sessionExists(sessionID: string): Promise<boolean> {
|
||||||
if (isSqliteBackend()) return true
|
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
|
return getMessageDir(sessionID) !== null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -70,7 +70,7 @@ export function createSessionManagerTools(ctx: PluginInput): Record<string, Tool
|
|||||||
},
|
},
|
||||||
execute: async (args: SessionReadArgs, _context) => {
|
execute: async (args: SessionReadArgs, _context) => {
|
||||||
try {
|
try {
|
||||||
if (!sessionExists(args.session_id)) {
|
if (!(await sessionExists(args.session_id))) {
|
||||||
return `Session not found: ${args.session_id}`
|
return `Session not found: ${args.session_id}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user