diff --git a/src/plugin-sdk/session-store-runtime.test.ts b/src/plugin-sdk/session-store-runtime.test.ts index 6e35a58ca76..9d8cbf9f76f 100644 --- a/src/plugin-sdk/session-store-runtime.test.ts +++ b/src/plugin-sdk/session-store-runtime.test.ts @@ -6,6 +6,7 @@ import { getSessionEntry, loadSessionStore, readSessionUpdatedAt, + resolveAndPersistSessionFile, saveSessionStore, updateSessionStore, upsertSessionEntry, @@ -145,4 +146,46 @@ describe("session-store-runtime compatibility", () => { }, ); }); + + it("preserves persisted transcript paths when resolving existing sessions", async () => { + await withOpenClawTestState( + { + layout: "state-only", + prefix: "openclaw-session-store-compat-", + scenario: "minimal", + }, + async (state) => { + const env = testEnv(state.stateDir); + const storePath = canonicalStorePath(state.stateDir); + const existingFile = path.join(state.stateDir, "legacy", "custom.jsonl"); + const fallbackFile = path.join( + state.stateDir, + "agents", + "main", + "sessions", + "fallback.jsonl", + ); + const sessionStore = { + "agent:main:main": { + sessionId: "existing-session", + sessionFile: existingFile, + updatedAt: 100, + sessionStartedAt: 100, + }, + }; + + const result = await resolveAndPersistSessionFile({ + sessionId: "existing-session", + sessionKey: "agent:main:main", + sessionStore, + storePath, + fallbackSessionFile: fallbackFile, + }); + + expect(result.sessionFile).toBe(existingFile); + const persisted = getSessionEntry({ agentId: "main", env, sessionKey: "agent:main:main" }); + expect((persisted as { sessionFile?: string } | undefined)?.sessionFile).toBe(existingFile); + }, + ); + }); }); diff --git a/src/plugin-sdk/session-store-runtime.ts b/src/plugin-sdk/session-store-runtime.ts index 27098942aad..103759b672c 100644 --- a/src/plugin-sdk/session-store-runtime.ts +++ b/src/plugin-sdk/session-store-runtime.ts @@ -270,8 +270,11 @@ export async function resolveAndPersistSessionFile(params: { updatedAt: now, sessionStartedAt: now, }; + const persistedSessionFile = + baseEntry.sessionId === params.sessionId ? baseEntry.sessionFile?.trim() : undefined; const sessionFile = - params.fallbackSessionFile?.trim() ?? + persistedSessionFile || + params.fallbackSessionFile?.trim() || resolveSessionTranscriptPathInDir( params.sessionId, params.sessionsDir ?? path.dirname(path.resolve(params.storePath)),